-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Check for cluster ID conflicts on handshake
In the heartbeat, nodes now share their cluster IDs and check that they match. We allow for missing cluster IDs, since new nodes do not have a cluster ID until they obtain one via gossip, but conflicting IDs will result in a heartbeat error. In addition, connections are now not added to the connection pool until the heartbeat succeeds. This allows us to fail fast when a node attempts to join the wrong cluster. Refers #18058.
- Loading branch information
1 parent
6695a57
commit a826bfb
Showing
47 changed files
with
634 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2017 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package base | ||
|
||
import ( | ||
"golang.org/x/net/context" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/cockroachdb/cockroach/pkg/util/syncutil" | ||
"github.com/cockroachdb/cockroach/pkg/util/uuid" | ||
) | ||
|
||
// ClusterIDContainer is used to share a single Cluster ID instance between | ||
// multiple layers. It allows setting and getting the value. Once a value is | ||
// set, the value cannot change. | ||
type ClusterIDContainer struct { | ||
syncutil.Mutex | ||
|
||
clusterID uuid.UUID | ||
} | ||
|
||
// String returns the cluster ID, or "?" if it is unset. | ||
func (c *ClusterIDContainer) String() string { | ||
val := c.Get() | ||
if val == uuid.Nil { | ||
return "?" | ||
} | ||
return val.String() | ||
} | ||
|
||
// Get returns the current cluster ID; uuid.Nil if it is unset. | ||
func (c *ClusterIDContainer) Get() uuid.UUID { | ||
c.Lock() | ||
defer c.Unlock() | ||
return c.clusterID | ||
} | ||
|
||
// Set sets the current cluster ID. If it is already set, the value must match. | ||
func (c *ClusterIDContainer) Set(ctx context.Context, val uuid.UUID) { | ||
c.Lock() | ||
defer c.Unlock() | ||
if c.clusterID == uuid.Nil { | ||
c.clusterID = val | ||
if log.V(2) { | ||
log.Infof(ctx, "ClusterID set to %s", val) | ||
} | ||
} else if c.clusterID != val { | ||
log.Fatalf(ctx, "different ClusterIDs set: %s, then %s", c.clusterID, val) | ||
} | ||
} | ||
|
||
// Reset changes the ClusterID regardless of the old value. | ||
// | ||
// Should only be used in testing code. | ||
func (c *ClusterIDContainer) Reset(val uuid.UUID) { | ||
c.Lock() | ||
defer c.Unlock() | ||
c.clusterID = val | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2017 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/uuid" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/base" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func TestClusterIDContainerEmpty(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
c := &base.ClusterIDContainer{} | ||
|
||
if val := c.Get(); val != uuid.Nil { | ||
t.Errorf("initial value should be uuid.Nil, not %s", val) | ||
} | ||
if str := c.String(); str != "?" { | ||
t.Errorf("initial string should be ?, not %s", str) | ||
} | ||
} | ||
|
||
func TestClusterIDContainerSet(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
c := &base.ClusterIDContainer{} | ||
u := uuid.MakeV4() | ||
|
||
for i := 0; i < 2; i++ { | ||
c.Set(context.Background(), u) | ||
if val := c.Get(); val != u { | ||
t.Errorf("value should be %s, not %s", u, val) | ||
} | ||
if str := c.String(); str != u.String() { | ||
t.Errorf("string should be %s, not %s", u.String(), str) | ||
} | ||
} | ||
} | ||
|
||
func TestClusterIDContainerReset(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
c := &base.ClusterIDContainer{} | ||
uuid1 := uuid.MakeV4() | ||
uuid2 := uuid.MakeV4() | ||
|
||
c.Set(context.Background(), uuid1) | ||
c.Reset(uuid2) | ||
if val := c.Get(); val != uuid2 { | ||
t.Errorf("value should be %s, not %s", uuid2, val) | ||
} | ||
if str := c.String(); str != uuid2.String() { | ||
t.Errorf("string should be %s, not %s", uuid2.String(), str) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.