@@ -11,7 +11,6 @@ import (
1111 "strconv"
1212 "time"
1313
14- "github.com/golang/snappy"
1514 clientv3 "go.etcd.io/etcd/client/v3"
1615 "go.uber.org/fx"
1716
@@ -24,26 +23,6 @@ import (
2423 "github.com/uber/cadence/service/sharddistributor/store/etcd/executorstore/shardcache"
2524)
2625
27- var (
28- _executorStatusRunningJSON = fmt .Sprintf (`"%s"` , types .ExecutorStatusACTIVE )
29- _executorStatusRunningJSONCompressed string
30- )
31-
32- func init () {
33- compressed , _ := compressJSON ([]byte (_executorStatusRunningJSON ))
34- _executorStatusRunningJSONCompressed = string (compressed )
35- }
36-
37- // compressJSON compresses JSON data using snappy compression
38- func compressJSON (data []byte ) ([]byte , error ) {
39- return snappy .Encode (nil , data ), nil
40- }
41-
42- // decompressJSON decompresses snappy-compressed data
43- func decompressJSON (data []byte ) ([]byte , error ) {
44- return snappy .Decode (nil , data )
45- }
46-
4726type executorStoreImpl struct {
4827 client * clientv3.Client
4928 prefix string
@@ -139,12 +118,12 @@ func (s *executorStoreImpl) RecordHeartbeat(ctx context.Context, namespace, exec
139118 }
140119
141120 // Compress data before writing to etcd
142- compressedReportedShards , err := compressJSON (reportedShardsData )
121+ compressedReportedShards , err := compress (reportedShardsData )
143122 if err != nil {
144123 return fmt .Errorf ("compress reported shards: %w" , err )
145124 }
146125
147- compressedState , err := compressJSON (jsonState )
126+ compressedState , err := compress (jsonState )
148127 if err != nil {
149128 return fmt .Errorf ("compress state: %w" , err )
150129 }
@@ -199,31 +178,16 @@ func (s *executorStoreImpl) GetHeartbeat(ctx context.Context, namespace string,
199178 }
200179 heartbeatState .LastHeartbeat = timestamp
201180 case etcdkeys .ExecutorStatusKey :
202- decompressed , err := decompressJSON (kv .Value )
203- if err != nil {
204- return nil , nil , fmt .Errorf ("decompress heartbeat state: %w" , err )
205- }
206- err = json .Unmarshal (decompressed , & heartbeatState .Status )
207- if err != nil {
208- return nil , nil , fmt .Errorf ("parse heartbeat state: %w" , err )
181+ if err := decompressAndUnmarshal (kv .Value , & heartbeatState .Status , "heartbeat state" ); err != nil {
182+ return nil , nil , err
209183 }
210184 case etcdkeys .ExecutorReportedShardsKey :
211- decompressed , err := decompressJSON (kv .Value )
212- if err != nil {
213- return nil , nil , fmt .Errorf ("decompress reported shards: %w" , err )
214- }
215- err = json .Unmarshal (decompressed , & heartbeatState .ReportedShards )
216- if err != nil {
217- return nil , nil , fmt .Errorf ("unmarshal reported shards: %w" , err )
185+ if err := decompressAndUnmarshal (kv .Value , & heartbeatState .ReportedShards , "reported shards" ); err != nil {
186+ return nil , nil , err
218187 }
219188 case etcdkeys .ExecutorAssignedStateKey :
220- decompressed , err := decompressJSON (kv .Value )
221- if err != nil {
222- return nil , nil , fmt .Errorf ("decompress assigned state: %w" , err )
223- }
224- err = json .Unmarshal (decompressed , & assignedState )
225- if err != nil {
226- return nil , nil , fmt .Errorf ("unmarshal assigned shards: %w" , err )
189+ if err := decompressAndUnmarshal (kv .Value , & assignedState , "assigned state" ); err != nil {
190+ return nil , nil , err
227191 }
228192 }
229193 }
@@ -262,31 +226,16 @@ func (s *executorStoreImpl) GetState(ctx context.Context, namespace string) (*st
262226 timestamp , _ := strconv .ParseInt (value , 10 , 64 )
263227 heartbeat .LastHeartbeat = timestamp
264228 case etcdkeys .ExecutorStatusKey :
265- decompressed , err := decompressJSON (kv .Value )
266- if err != nil {
267- return nil , fmt .Errorf ("decompress heartbeat state: %w" , err )
268- }
269- err = json .Unmarshal (decompressed , & heartbeat .Status )
270- if err != nil {
271- return nil , fmt .Errorf ("parse heartbeat state: %w" , err )
229+ if err := decompressAndUnmarshal (kv .Value , & heartbeat .Status , "heartbeat state" ); err != nil {
230+ return nil , err
272231 }
273232 case etcdkeys .ExecutorReportedShardsKey :
274- decompressed , err := decompressJSON (kv .Value )
275- if err != nil {
276- return nil , fmt .Errorf ("decompress reported shards: %w" , err )
277- }
278- err = json .Unmarshal (decompressed , & heartbeat .ReportedShards )
279- if err != nil {
280- return nil , fmt .Errorf ("unmarshal reported shards: %w" , err )
233+ if err := decompressAndUnmarshal (kv .Value , & heartbeat .ReportedShards , "reported shards" ); err != nil {
234+ return nil , err
281235 }
282236 case etcdkeys .ExecutorAssignedStateKey :
283- decompressed , err := decompressJSON (kv .Value )
284- if err != nil {
285- return nil , fmt .Errorf ("decompress assigned state: %w" , err )
286- }
287- err = json .Unmarshal (decompressed , & assigned )
288- if err != nil {
289- return nil , fmt .Errorf ("unmarshal assigned shards: %w" , err )
237+ if err := decompressAndUnmarshal (kv .Value , & assigned , "assigned state" ); err != nil {
238+ return nil , err
290239 }
291240 assigned .ModRevision = kv .ModRevision
292241 }
@@ -358,7 +307,7 @@ func (s *executorStoreImpl) AssignShards(ctx context.Context, namespace string,
358307 if err != nil {
359308 return fmt .Errorf ("marshal assigned shards for executor %s: %w" , executorID , err )
360309 }
361- compressedValue , err := compressJSON (value )
310+ compressedValue , err := compress (value )
362311 if err != nil {
363312 return fmt .Errorf ("compress assigned shards for executor %s: %w" , executorID , err )
364313 }
@@ -441,12 +390,8 @@ func (s *executorStoreImpl) AssignShard(ctx context.Context, namespace, shardID,
441390 // If the executor already has shards, load its state.
442391 kv := resp .Kvs [0 ]
443392 modRevision = kv .ModRevision
444- decompressed , err := decompressJSON (kv .Value )
445- if err != nil {
446- return fmt .Errorf ("decompress assigned state: %w" , err )
447- }
448- if err := json .Unmarshal (decompressed , & state ); err != nil {
449- return fmt .Errorf ("unmarshal assigned state: %w" , err )
393+ if err := decompressAndUnmarshal (kv .Value , & state , "assigned state" ); err != nil {
394+ return err
450395 }
451396 } else {
452397 // If this is the first shard, initialize the state map.
@@ -463,7 +408,7 @@ func (s *executorStoreImpl) AssignShard(ctx context.Context, namespace, shardID,
463408 return fmt .Errorf ("marshal new assigned state: %w" , err )
464409 }
465410
466- compressedStateValue , err := compressJSON (newStateValue )
411+ compressedStateValue , err := compress (newStateValue )
467412 if err != nil {
468413 return fmt .Errorf ("compress new assigned state: %w" , err )
469414 }
@@ -472,7 +417,7 @@ func (s *executorStoreImpl) AssignShard(ctx context.Context, namespace, shardID,
472417
473418 // 3. Prepare and commit the transaction with three atomic checks.
474419 // a) Check that the executor's status is ACTIVE.
475- comparisons = append (comparisons , clientv3 .Compare (clientv3 .Value (statusKey ), "=" , _executorStatusRunningJSONCompressed ))
420+ comparisons = append (comparisons , clientv3 .Compare (clientv3 .Value (statusKey ), "=" , compressedActiveStatus () ))
476421 // b) Check that the assigned_state key hasn't been changed by another process.
477422 comparisons = append (comparisons , clientv3 .Compare (clientv3 .ModRevision (assignedState ), "=" , modRevision ))
478423 // c) Check that the cache is up to date.
@@ -510,9 +455,9 @@ func (s *executorStoreImpl) AssignShard(ctx context.Context, namespace, shardID,
510455 if err != nil || len (currentStatusResp .Kvs ) == 0 {
511456 return store .ErrExecutorNotFound
512457 }
513- if string (currentStatusResp .Kvs [0 ].Value ) != _executorStatusRunningJSONCompressed {
458+ if string (currentStatusResp .Kvs [0 ].Value ) != compressedActiveStatus () {
514459 // Decompress the status for error message
515- decompressedStatus , _ := decompressJSON (currentStatusResp .Kvs [0 ].Value )
460+ decompressedStatus , _ := decompress (currentStatusResp .Kvs [0 ].Value )
516461 return fmt .Errorf (`%w: executor status is %s"` , store .ErrVersionConflict , string (decompressedStatus ))
517462 }
518463
0 commit comments