-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test helper for creating clustered stores
- Loading branch information
1 parent
653a4b5
commit 1cc568a
Showing
7 changed files
with
131 additions
and
43 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
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,118 @@ | ||
/* | ||
Copyright 2023 Avi Zimmerman <[email protected]> | ||
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 store | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// NewTestStore creates a new test store and waits for it to be ready. | ||
// The context is used to enforce startup timeouts. | ||
func NewTestStore(ctx context.Context) (Store, error) { | ||
st, err := New(newTestOptions(ctx)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stor := st.(*store) | ||
stor.testStore = true | ||
if err := stor.Open(); err != nil { | ||
return nil, err | ||
} | ||
err = <-stor.ReadyError(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return stor, nil | ||
} | ||
|
||
// NewTestCluster creates a new test cluster and waits for it to be ready. | ||
// The context is used to enforce startup timeouts. Clusters cannot be | ||
// created in parallel without specifying unique raft ports. If startPort | ||
// is 0, a default port will be used. The number of nodes must be greater | ||
// than 0. | ||
func NewTestCluster(ctx context.Context, numNodes int, startPort int) ([]Store, error) { | ||
const defaultStartPort = 10000 | ||
if startPort == 0 { | ||
startPort = defaultStartPort | ||
} | ||
if numNodes < 1 { | ||
return nil, errors.New("invalid number of nodes") | ||
} | ||
bootstrapServers := make([]string, numNodes) | ||
for i := 0; i < numNodes; i++ { | ||
bootstrapServers[i] = fmt.Sprintf("node-%d=127.0.0.1:%d", i, startPort+i) | ||
} | ||
opts := make([]*Options, numNodes) | ||
for i := 0; i < numNodes; i++ { | ||
thisID := fmt.Sprintf("node-%d", i) | ||
thisAddr := fmt.Sprintf("127.0.0.1:%d", startPort+i) | ||
opts[i] = newTestOptions(ctx) | ||
opts[i].Mesh.NodeID = thisID | ||
opts[i].Bootstrap.AdvertiseAddress = thisAddr | ||
opts[i].Bootstrap.Servers = strings.Join(bootstrapServers, ",") | ||
opts[i].Raft.ListenAddress = thisAddr | ||
} | ||
stores := make([]Store, numNodes) | ||
for i := 0; i < numNodes; i++ { | ||
st, err := New(opts[i]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stor := st.(*store) | ||
stor.testStore = true | ||
stores[i] = stor | ||
} | ||
g, ctx := errgroup.WithContext(ctx) | ||
for i := 0; i < numNodes; i++ { | ||
i := i | ||
g.Go(func() error { | ||
if err := stores[i].Open(); err != nil { | ||
return err | ||
} | ||
return <-stores[i].ReadyError(ctx) | ||
}) | ||
} | ||
if err := g.Wait(); err != nil { | ||
return nil, err | ||
} | ||
return stores, nil | ||
} | ||
|
||
func newTestOptions(ctx context.Context) *Options { | ||
opts := NewOptions() | ||
opts.Raft.ConnectionTimeout = 100 * time.Millisecond | ||
opts.Raft.HeartbeatTimeout = 100 * time.Millisecond | ||
opts.Raft.ElectionTimeout = 100 * time.Millisecond | ||
opts.Raft.LeaderLeaseTimeout = 100 * time.Millisecond | ||
opts.Raft.ListenAddress = ":0" | ||
opts.Raft.InMemory = true | ||
opts.TLS.Insecure = true | ||
opts.Bootstrap.Enabled = true | ||
opts.Mesh.NodeID = uuid.NewString() | ||
deadline, ok := ctx.Deadline() | ||
if ok { | ||
opts.Raft.StartupTimeout = time.Until(deadline) | ||
} | ||
return opts | ||
} |