forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement MongoDB plugin Part1: skeleton and ConfigStore (cadence-wo…
- Loading branch information
1 parent
d61a3b3
commit 46b84be
Showing
35 changed files
with
1,502 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2021 Uber Technologies, Inc. | ||
// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package nosqlplugin | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
func getCadencePackageDir() (string, error) { | ||
cadencePackageDir, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
cadenceIndex := strings.LastIndex(cadencePackageDir, "/cadence/") | ||
cadencePackageDir = cadencePackageDir[:cadenceIndex+len("/cadence/")] | ||
return cadencePackageDir, err | ||
} | ||
|
||
func GetDefaultTestSchemaDir(testSchemaRelativePath string) (string, error) { | ||
cadencePackageDir, err := getCadencePackageDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
return cadencePackageDir + testSchemaRelativePath, nil | ||
} |
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 (c) 2021 Uber Technologies, Inc. | ||
// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
|
||
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin" | ||
) | ||
|
||
var _ nosqlplugin.AdminDB = (*mdb)(nil) | ||
|
||
const ( | ||
testSchemaDir = "schema/mongodb/" | ||
) | ||
|
||
func (db *mdb) SetupTestDatabase(schemaBaseDir string) error { | ||
if schemaBaseDir == "" { | ||
var err error | ||
schemaBaseDir, err = nosqlplugin.GetDefaultTestSchemaDir(testSchemaDir) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
schemaFile := schemaBaseDir + "cadence/schema.json" | ||
byteValues, err := ioutil.ReadFile(schemaFile) | ||
if err != nil { | ||
return err | ||
} | ||
var commands []interface{} | ||
err = bson.UnmarshalExtJSON(byteValues, false, &commands) | ||
if err != nil { | ||
return err | ||
} | ||
for _, cmd := range commands { | ||
result := db.dbConn.RunCommand(context.Background(), cmd) | ||
if result.Err() != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (db *mdb) TeardownTestDatabase() error { | ||
result := db.dbConn.RunCommand(context.Background(), bson.D{{"dropDatabase", 1}}) | ||
err := result.Err() | ||
return err | ||
} |
70 changes: 70 additions & 0 deletions
70
common/persistence/nosql/nosqlplugin/mongodb/configStore.go
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,70 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
|
||
"github.com/uber/cadence/common" | ||
"github.com/uber/cadence/common/persistence" | ||
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin" | ||
"github.com/uber/cadence/schema/mongodb/cadence" | ||
) | ||
|
||
func (db *mdb) InsertConfig(ctx context.Context, row *persistence.InternalConfigStoreEntry) error { | ||
collection := db.dbConn.Collection(cadence.ClusterConfigCollectionName) | ||
doc := cadence.ClusterConfigCollectionEntry{ | ||
RowType: row.RowType, | ||
Version: row.Version, | ||
UnixTimestampSeconds: row.Timestamp.Unix(), | ||
Data: row.Values.Data, | ||
DataEncoding: row.Values.GetEncodingString(), | ||
} | ||
_, err := collection.InsertOne(ctx, doc) | ||
if mongo.IsDuplicateKeyError(err) { | ||
return nosqlplugin.NewConditionFailure("InsertConfig operation failed because of version collision") | ||
} | ||
return err | ||
} | ||
|
||
func (db *mdb) SelectLatestConfig(ctx context.Context, rowType int) (*persistence.InternalConfigStoreEntry, error) { | ||
filter := bson.D{{"rowtype", rowType}} | ||
queryOptions := options.FindOneOptions{} | ||
queryOptions.SetSort(bson.D{{"version", -1}}) | ||
|
||
collection := db.dbConn.Collection(cadence.ClusterConfigCollectionName) | ||
var result cadence.ClusterConfigCollectionEntry | ||
err := collection.FindOne(ctx, filter, &queryOptions).Decode(&result) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &persistence.InternalConfigStoreEntry{ | ||
RowType: rowType, | ||
Version: result.Version, | ||
Timestamp: time.Unix(result.UnixTimestampSeconds, 0), | ||
Values: persistence.NewDataBlob(result.Data, common.EncodingType(result.DataEncoding)), | ||
}, nil | ||
} |
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,49 @@ | ||
// Copyright (c) 2020 Uber Technologies, Inc. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package mongodb | ||
|
||
import ( | ||
"context" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
|
||
"github.com/uber/cadence/common/config" | ||
"github.com/uber/cadence/common/log" | ||
"github.com/uber/cadence/common/persistence/nosql/nosqlplugin" | ||
) | ||
|
||
// mdb represents a logical connection to MongoDB database | ||
type mdb struct { | ||
client *mongo.Client | ||
dbConn *mongo.Database | ||
cfg *config.NoSQL | ||
logger log.Logger | ||
} | ||
|
||
var _ nosqlplugin.DB = (*mdb)(nil) | ||
|
||
func (db *mdb) Close() { | ||
db.client.Disconnect(context.Background()) | ||
} | ||
|
||
func (db *mdb) PluginName() string { | ||
return PluginName | ||
} |
Oops, something went wrong.