@@ -25,6 +25,9 @@ package main
2525import (
2626 "encoding/json"
2727 "errors"
28+ "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
29+ "strings"
30+
2831 //"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
2932 "github.com/thumbtack/go/lib/metrics"
3033 //"github.com/thumbtack/go/lib/monitoring"
@@ -33,7 +36,6 @@ import (
3336 _ "net/http/pprof"
3437 "os"
3538 "strconv"
36- "strings"
3739 "sync"
3840 "time"
3941
@@ -49,18 +51,16 @@ const (
4951 paramCheckpointTable = "CHECKPOINT_DDB_TABLE"
5052 paramCheckpointRegion = "CHECKPOINT_DDB_REGION"
5153 paramCheckpointEndpoint = "CHECKPOINT_DDB_ENDPOINT"
52- paramMaxRetries = "MAX_RETRIES"
5354 paramVerbose = "VERBOSE"
5455 paramPort = "PORT"
5556 paramConfigDir = "CONFIG_DIR"
56- defaultConfigMaxRetries = 3
57+ maxRetries = 3
5758)
5859
5960var logger = logging .New ()
6061var ddbTable = os .Getenv (paramCheckpointTable )
6162var ddbRegion = os .Getenv (paramCheckpointRegion )
6263var ddbEndpoint = os .Getenv (paramCheckpointEndpoint )
63- var maxRetries = defaultConfigMaxRetries
6464var ddbClient = ddbConfigConnect (ddbRegion , ddbEndpoint , maxRetries , * logger )
6565var metricsClient = newMetricsClient ()
6666
@@ -73,16 +73,12 @@ type config struct {
7373 DstEndpoint string `json:"dst_endpoint"`
7474 SrcEnv string `json:"src_env"`
7575 DstEnv string `json:"dst_env"`
76- SrcAccount string `json:"src_account"`
77- DstAccount string `json:"dst_account"`
78- MaxConnectRetries int `json:"max_connect_retries"`
7976 ReadWorkers int `json:"read_workers"`
8077 WriteWorkers int `json:"write_workers"`
8178 ReadQps int64 `json:"read_qps"`
8279 WriteQps int64 `json:"write_qps"`
8380 UpdateCheckpointThreshold int `json:"update_checkpoint_threshold"`
8481 EnableStreaming * bool `json:"enable_streaming"`
85- TruncateTable bool `json:"truncate_table"`
8682}
8783
8884// Config file is read and dumped into this struct
@@ -101,13 +97,8 @@ type syncState struct {
10197 timestamp time.Time
10298}
10399
104- func getRoleArn (env string , account string ) (string ) {
105- var roleType = ""
106- if strings .ToLower (account ) != "admin" {
107- roleType = "NEW_" + strings .ToUpper (env ) + "_ROLE"
108- } else {
109- roleType = "OLD_" + strings .ToUpper (env ) + "_ROLE"
110- }
100+ func getRoleArn (env string ) string {
101+ roleType := strings .ToUpper (env ) + "_ROLE"
111102 logger .WithFields (logging.Fields {"Roletype" : roleType }).Debug ()
112103 return os .Getenv (roleType )
113104}
@@ -130,7 +121,7 @@ func NewSyncState(tableConfig config) *syncState {
130121 aws .NewConfig ().
131122 WithRegion (tableConfig .SrcRegion ).
132123 WithEndpoint (tableConfig .SrcEndpoint ).
133- WithMaxRetries (tableConfig . MaxConnectRetries ).
124+ WithMaxRetries (maxRetries ).
134125 WithHTTPClient (httpClient ),
135126 ))
136127
@@ -139,10 +130,10 @@ func NewSyncState(tableConfig config) *syncState {
139130 aws .NewConfig ().
140131 WithRegion (tableConfig .DstRegion ).
141132 WithEndpoint (tableConfig .DstEndpoint ).
142- WithMaxRetries (tableConfig . MaxConnectRetries ),
133+ WithMaxRetries (maxRetries ),
143134 ))
144- srcRoleArn := getRoleArn (tableConfig .SrcEnv , tableConfig . SrcAccount )
145- dstRoleArn := getRoleArn (tableConfig .DstEnv , tableConfig . DstAccount )
135+ srcRoleArn := getRoleArn (tableConfig .SrcEnv )
136+ dstRoleArn := getRoleArn (tableConfig .DstEnv )
146137
147138 if srcRoleArn == "" || dstRoleArn == "" {
148139 logger .WithFields (logging.Fields {}).
@@ -154,16 +145,12 @@ func NewSyncState(tableConfig config) *syncState {
154145 "Src Role Arn" : srcRoleArn ,
155146 "Dst Role Arn" : dstRoleArn }).Debug ("Role ARN" )
156147
157- /* srcCreds := stscreds.NewCredentials(srcSess, srcRoleArn)
148+ srcCreds := stscreds .NewCredentials (srcSess , srcRoleArn )
158149 dstCreds := stscreds .NewCredentials (dstSess , dstRoleArn )
159150
160151 srcDynamo = dynamodb .New (srcSess , & aws.Config {Credentials : srcCreds })
161152 dstDynamo = dynamodb .New (dstSess , & aws.Config {Credentials : dstCreds })
162- stream = dynamodbstreams.New(srcSess, &aws.Config{Credentials: srcCreds})*/
163-
164- srcDynamo = dynamodb .New (srcSess , & aws.Config {})
165- dstDynamo = dynamodb .New (dstSess , & aws.Config {})
166- stream = dynamodbstreams .New (srcSess , & aws.Config {})
153+ stream = dynamodbstreams .New (srcSess , & aws.Config {Credentials : srcCreds })
167154
168155 return & syncState {
169156 tableConfig : tableConfig ,
@@ -238,14 +225,6 @@ func NewApp() *appConfig {
238225 logger .SetLevel (logging .DebugLevel )
239226 }
240227 }
241- if os .Getenv (paramMaxRetries ) != "" {
242- maxRetries , err = strconv .Atoi (os .Getenv (paramMaxRetries ))
243- if err != nil {
244- logger .WithFields (logging.Fields {
245- "error" : err ,
246- }).Fatal ("Failed to parse " + paramMaxRetries )
247- }
248- }
249228
250229 configFile = os .Getenv (paramConfigDir ) + "/config.json"
251230 tableConfig , err := readConfigFile (configFile , * logger )
@@ -298,17 +277,6 @@ func setDefaults(tableConfig []config) ([]config, error) {
298277 continue
299278 }
300279
301- if tableConfig [i ].SrcAccount == "" {
302- tableConfig [i ].SrcAccount = "admin"
303- }
304-
305- if tableConfig [i ].DstAccount == "" {
306- tableConfig [i ].DstAccount = "admin"
307- }
308-
309- if tableConfig [i ].MaxConnectRetries == 0 {
310- tableConfig [i ].MaxConnectRetries = 3
311- }
312280
313281 if tableConfig [i ].ReadQps == 0 {
314282 tableConfig [i ].ReadQps = 500
@@ -354,18 +322,22 @@ func (sync *syncState) isFreshStart(key primaryKey) bool {
354322 return false
355323}
356324
357- func getPrimaryKey (sync config ) ( primaryKey ) {
325+ func getPrimaryKey (sync config ) primaryKey {
358326 key := primaryKey {}
359- if sync .SrcAccount == "admin" {
327+ delim := "_"
328+
329+ if ! strings .Contains (sync .SrcEnv , " new" ) {
360330 key .sourceTable = sync .SrcTable
361331 } else {
362- key .sourceTable = sync .SrcTable + ".account." + sync .SrcAccount
332+ key .sourceTable = sync .SrcTable + ".account." + strings . Split ( sync .SrcEnv , delim )[ 0 ]
363333 }
364- if sync .DstAccount == "admin" {
334+
335+ if ! strings .Contains (sync .DstEnv , "new" ) {
365336 key .dstTable = sync .DstTable
366337 } else {
367- key .dstTable = sync .DstTable + ".account." + sync .DstAccount
338+ key .dstTable = sync .DstTable + ".account." + strings . Split ( sync .DstEnv , delim )[ 0 ]
368339 }
340+
369341 return key
370342}
371343
@@ -389,8 +361,9 @@ func main() {
389361 "Source Table" : key .sourceTable ,
390362 "Destination Table" : key .dstTable ,
391363 }).Error ("Error in connecting to tables. Check config file" )
392-
364+ return
393365 }
366+
394367 syncWorker .readCheckpoint ()
395368
396369 // Call a go routine to replicate for each key
0 commit comments