Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Privateclusterfile #2204

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions fdbclient/admin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,11 @@ func (client *cliAdminClient) ChangeCoordinators(addresses []fdbv1beta2.ProcessA
if err != nil {
return "", err
}

connectionStringBytes, err := os.ReadFile(client.clusterFilePath)
if err != nil {
return "", err
}

connectionString, err := fdbv1beta2.ParseConnectionString(string(connectionStringBytes))
connectionString, err := client.GetConnectionString()
if err != nil {
return "", err
}
return connectionString.String(), nil
return connectionString, nil
}

// cleanConnectionStringOutput is a helper method to remove unrelated output from the get command in the connection string
Expand Down Expand Up @@ -687,8 +681,7 @@ func (client *cliAdminClient) GetRestoreStatus() (string, error) {

// Close cleans up any pending resources.
func (client *cliAdminClient) Close() error {
// Allow to reuse the same file.
return nil
return os.Remove(client.clusterFilePath)
}

// GetCoordinatorSet gets the current coordinators from the status
Expand Down
69 changes: 40 additions & 29 deletions fdbclient/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ import (
"errors"
"fmt"
"io/fs"
"k8s.io/apimachinery/pkg/types"
"math/rand"
"os"
"path"
"sync"
"time"

fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
Expand All @@ -42,6 +45,10 @@ var DefaultCLITimeout = 10 * time.Second
// MaxCliTimeout is the maximum CLI timeout that will be used for requests that might be slower to respond.
var MaxCliTimeout = 40 * time.Second

// Keeps a singleton databases. The key is the cluster UID. Guarded by the mutex below for read and write access.
var databaseSingleton map[types.UID]*fdb.Database
var databaseSingletonMutex = &sync.Mutex{}

const (
defaultTransactionTimeout = 5 * time.Second
)
Expand Down Expand Up @@ -83,24 +90,29 @@ func parseMachineReadableStatus(logger logr.Logger, contents []byte, checkForPro
return status, nil
}

// getFDBDatabase opens an FDB database.
// getFDBDatabase returns the singleton FDB database. May return an error if initializing the singleton failed.
func getFDBDatabase(cluster *fdbv1beta2.FoundationDBCluster) (fdb.Database, error) {
clusterFile, err := createClusterFile(cluster)
if err != nil {
return fdb.Database{}, err
}
databaseSingletonMutex.Lock()
defer databaseSingletonMutex.Unlock()
if databaseSingleton[cluster.UID] == nil {
clusterFile, err := createClusterFile(cluster)
if err != nil {
return fdb.Database{}, err
}

database, err := fdb.OpenDatabase(clusterFile)
if err != nil {
return fdb.Database{}, err
}
database, err := fdb.OpenDatabase(clusterFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use OpenWithConnectionString to bypass the other cache on Go SDK side; was briefly discussed with @johscheuer here

if err != nil {
return fdb.Database{}, err
}

err = database.Options().SetTransactionTimeout(defaultTransactionTimeout.Milliseconds())
if err != nil {
return fdb.Database{}, err
err = database.Options().SetTransactionTimeout(defaultTransactionTimeout.Milliseconds())
if err != nil {
return fdb.Database{}, err
}
databaseSingleton[cluster.UID] = &database
}

return database, nil
// This is a copy, but fdb.Database is just a pointer-to-implementation and cheap to copy.
return *databaseSingleton[cluster.UID], nil
}

// createClusterFile will create or update the cluster file for the specified cluster.
Expand All @@ -110,23 +122,22 @@ func createClusterFile(cluster *fdbv1beta2.FoundationDBCluster) (string, error)

// ensureClusterFileIsPresent will ensure that the cluster file with the specified connection string is present.
func ensureClusterFileIsPresent(dir string, uid string, connectionString string) (string, error) {
clusterFileName := path.Join(dir, uid)

// Try to read the file to check if the file already exists and if so, if the content matches
content, err := os.ReadFile(clusterFileName)

// If the file doesn't exist we have to create it
if errors.Is(err, fs.ErrNotExist) {
return clusterFileName, os.WriteFile(clusterFileName, []byte(connectionString), 0777)
}
for {
clusterFileName := path.Join(dir, fmt.Sprintf("%s-%d", uid, rand.Uint64()))

// The content of the cluster file is already correct.
if string(content) == connectionString {
return clusterFileName, nil
f, err := os.OpenFile(clusterFileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0777)
if err != nil {
if errors.Is(err, fs.ErrExist) {
continue
}
return "", err
}
_, err = f.Write([]byte(connectionString))
if err1 := f.Close(); err1 != nil && err == nil {
err = err1
}
return clusterFileName, err
}

// The content doesn't match, so we have to write the new content to the cluster file.
return clusterFileName, os.WriteFile(clusterFileName, []byte(connectionString), 0777)
}

// getConnectionStringFromDB gets the database's connection string directly from the system key
Expand Down
30 changes: 1 addition & 29 deletions fdbclient/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,7 @@ var _ = Describe("common_test", func() {

When("the cluster file doesn't exist", func() {
It("should create the cluster file with the correct content", func() {
Expect(clusterFile).To(Equal(path.Join(tmpDir, uid)))
content, err := os.ReadFile(clusterFile)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(connectionString))
})
})

When("the cluster file exist with the wrong content", func() {
BeforeEach(func() {
err := os.WriteFile(path.Join(os.TempDir(), uid), []byte("wrong"), 0777)
Expect(err).NotTo(HaveOccurred())
})

It("should update the cluster file with the correct content", func() {
Expect(clusterFile).To(Equal(path.Join(tmpDir, uid)))
content, err := os.ReadFile(clusterFile)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(connectionString))
})
})

When("the cluster file exist with the correct content", func() {
BeforeEach(func() {
err := os.WriteFile(path.Join(os.TempDir(), uid), []byte(connectionString), 0777)
Expect(err).NotTo(HaveOccurred())
})

It("should keep the cluster file with the correct content", func() {
Expect(clusterFile).To(Equal(path.Join(tmpDir, uid)))
Expect(clusterFile).To(HavePrefix(path.Join(tmpDir, uid+"-")))
content, err := os.ReadFile(clusterFile)
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal(connectionString))
Expand Down