Skip to content

Commit b80d569

Browse files
fafgFabiano Graças
and
Fabiano Graças
authored
Upgrade golang 1.17 (#98)
* upgrade to golang 1.17 Signed-off-by: Fabiano Graças <[email protected]> # Conflicts: # go.mod # go.sum * improve after shell lint Signed-off-by: Fabiano Graças <[email protected]> * improve after upgrade docker image (used by the build system) Signed-off-by: Fabiano Graças <[email protected]> * remove not needed variable Signed-off-by: Fabiano Graças <[email protected]> * apply fixes after security scan (hmake test) Signed-off-by: Fabiano Graças <[email protected]> * add missing package after merge with latest master branch code. Signed-off-by: Fabiano Graças <[email protected]> * improve docker layer Signed-off-by: Fabiano Graças <[email protected]> * upgrade packages Signed-off-by: Fabiano Graças <[email protected]> Co-authored-by: Fabiano Graças <[email protected]>
1 parent 1df73a4 commit b80d569

File tree

10 files changed

+524
-115
lines changed

10 files changed

+524
-115
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@
1515
.vscode
1616
*_mock_test.go
1717
filenames
18+
19+
.DS_Store

HyperMake

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ targets:
2121
watches:
2222
- go.mod
2323
cmds:
24-
- export GO111MODULE=on
2524
- go mod download
2625
- go mod vendor
2726
- go mod tidy
@@ -88,5 +87,5 @@ settings:
8887
default-targets:
8988
- test
9089
docker:
91-
image: 'vmware/go-kcl-toolchain:0.1.3'
90+
image: 'vmware/go-kcl-toolchain:0.1.4'
9291
src-volume: /go/src/github.com/vmware/vmware-go-kcl

clientlibrary/utils/random.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1717
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
*/
19+
20+
// Package utils
1921
package utils
2022

2123
import (
22-
"math/rand"
24+
"crypto/rand"
25+
"math/big"
2326
"time"
2427
)
2528

@@ -32,11 +35,13 @@ const (
3235

3336
func RandStringBytesMaskImpr(n int) string {
3437
b := make([]byte, n)
35-
rand.Seed(time.Now().UTC().UnixNano())
36-
// A rand.Int63() generates 63 random bits, enough for letterIdxMax letters!
37-
for i, cache, remain := n-1, rand.Int63(), letterIdxMax; i >= 0; {
38+
seed := time.Now().UTC().UnixNano()
39+
rnd, _ := rand.Int(rand.Reader, big.NewInt(seed))
40+
// A rand.Int64() generates 64 random bits, enough for letterIdxMax letters!
41+
for i, cache, remain := n-1, rnd.Int64(), letterIdxMax; i >= 0; {
3842
if remain == 0 {
39-
cache, remain = rand.Int63(), letterIdxMax
43+
rnd, _ = rand.Int(rand.Reader, big.NewInt(seed))
44+
cache, remain = rnd.Int64(), letterIdxMax
4045
}
4146
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
4247
b[i] = letterBytes[idx]

clientlibrary/utils/random_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ package utils
2020

2121
import (
2222
"fmt"
23-
"math/rand"
2423
"testing"
2524
"time"
2625
)
@@ -32,17 +31,18 @@ func TestRandom(t *testing.T) {
3231
if s1 == s2 {
3332
t.Fatalf("failed in generating random string. s1: %s, s2: %s", s1, s2)
3433
}
34+
fmt.Println(s1)
35+
fmt.Println(s2)
3536
}
3637
}
3738

3839
func TestRandomNum(t *testing.T) {
39-
rand.Seed(time.Now().UTC().UnixNano())
40-
4140
for i := 0; i < 10; i++ {
42-
s1 := rand.Int63()
43-
s2 := rand.Int63()
41+
seed := time.Now().UTC().Second()
42+
s1 := RandStringBytesMaskImpr(seed)
43+
s2 := RandStringBytesMaskImpr(seed)
4444
if s1 == s2 {
45-
t.Fatalf("failed in generating random string. s1: %d, s2: %d", s1, s2)
45+
t.Fatalf("failed in generating random string. s1: %s, s2: %s", s1, s2)
4646
}
4747
fmt.Println(s1)
4848
fmt.Println(s2)

clientlibrary/worker/worker.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1717
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1818
*/
19+
20+
// Package worker
1921
// The implementation is derived from https://github.com/patrobinson/gokini
2022
//
2123
// Copyright 2018 Patrick robinson
@@ -28,8 +30,9 @@
2830
package worker
2931

3032
import (
33+
"crypto/rand"
3134
"errors"
32-
"math/rand"
35+
"math/big"
3336
"sync"
3437
"time"
3538

@@ -45,11 +48,9 @@ import (
4548
par "github.com/vmware/vmware-go-kcl/clientlibrary/partition"
4649
)
4750

48-
/**
49-
* Worker is the high level class that Kinesis applications use to start processing data. It initializes and oversees
50-
* different components (e.g. syncing shard and lease information, tracking shard assignments, and processing data from
51-
* the shards).
52-
*/
51+
//Worker is the high level class that Kinesis applications use to start processing data. It initializes and oversees
52+
//different components (e.g. syncing shard and lease information, tracking shard assignments, and processing data from
53+
//the shards).
5354
type Worker struct {
5455
streamName string
5556
regionName string
@@ -66,7 +67,7 @@ type Worker struct {
6667
waitGroup *sync.WaitGroup
6768
done bool
6869

69-
rng *rand.Rand
70+
randomSeed int64
7071

7172
shardStatus map[string]*par.ShardStatus
7273
shardStealInProgress bool
@@ -80,9 +81,6 @@ func NewWorker(factory kcl.IRecordProcessorFactory, kclConfig *config.KinesisCli
8081
mService = metrics.NoopMonitoringService{}
8182
}
8283

83-
// Create a pseudo-random number generator and seed it.
84-
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
85-
8684
return &Worker{
8785
streamName: kclConfig.StreamName,
8886
regionName: kclConfig.RegionName,
@@ -91,7 +89,7 @@ func NewWorker(factory kcl.IRecordProcessorFactory, kclConfig *config.KinesisCli
9189
kclConfig: kclConfig,
9290
mService: mService,
9391
done: false,
94-
rng: rng,
92+
randomSeed: time.Now().UTC().UnixNano(),
9593
}
9694
}
9795

@@ -108,7 +106,7 @@ func (w *Worker) WithCheckpointer(checker chk.Checkpointer) *Worker {
108106
return w
109107
}
110108

111-
// Run starts consuming data from the stream, and pass it to the application record processors.
109+
// Start Run starts consuming data from the stream, and pass it to the application record processors.
112110
func (w *Worker) Start() error {
113111
log := w.kclConfig.Logger
114112
if err := w.initialize(); err != nil {
@@ -133,7 +131,7 @@ func (w *Worker) Start() error {
133131
return nil
134132
}
135133

136-
// Shutdown signals worker to shutdown. Worker will try initiating shutdown of all record processors.
134+
// Shutdown signals worker to shut down. Worker will try initiating shutdown of all record processors.
137135
func (w *Worker) Shutdown() {
138136
log := w.kclConfig.Logger
139137
log.Infof("Worker shutdown in requested.")
@@ -258,7 +256,8 @@ func (w *Worker) eventLoop() {
258256
// starts at the same time, this decreases the probability of them calling
259257
// kinesis.DescribeStream at the same time, and hit the hard-limit on aws API calls.
260258
// On average the period remains the same so that doesn't affect behavior.
261-
shardSyncSleep := w.kclConfig.ShardSyncIntervalMillis/2 + w.rng.Intn(w.kclConfig.ShardSyncIntervalMillis)
259+
rnd, _ := rand.Int(rand.Reader, big.NewInt(int64(w.kclConfig.ShardSyncIntervalMillis)))
260+
shardSyncSleep := w.kclConfig.ShardSyncIntervalMillis/2 + int(rnd.Int64())
262261

263262
err := w.syncShard()
264263
if err != nil {
@@ -290,7 +289,7 @@ func (w *Worker) eventLoop() {
290289

291290
err := w.checkpointer.FetchCheckpoint(shard)
292291
if err != nil {
293-
// checkpoint may not existed yet is not an error condition.
292+
// checkpoint may not exist yet is not an error condition.
294293
if err != chk.ErrSequenceIDNotFound {
295294
log.Warnf("Couldn't fetch checkpoint: %+v", err)
296295
// move on to next shard
@@ -371,7 +370,7 @@ func (w *Worker) rebalance() error {
371370
return err
372371
}
373372

374-
// Only attempt to steal one shard at at time, to allow for linear convergence
373+
// Only attempt to steal one shard at time, to allow for linear convergence
375374
if w.shardStealInProgress {
376375
shardInfo := make(map[string]bool)
377376
err := w.getShardIDs("", shardInfo)
@@ -418,12 +417,12 @@ func (w *Worker) rebalance() error {
418417
log.Debugf("We have enough shards, not attempting to steal any. workerID: %s", w.workerID)
419418
return nil
420419
}
421-
maxShards := int(optimalShards)
420+
422421
var workerSteal string
423422
for worker, shards := range workers {
424-
if worker != w.workerID && len(shards) > maxShards {
423+
if worker != w.workerID && len(shards) > optimalShards {
425424
workerSteal = worker
426-
maxShards = len(shards)
425+
optimalShards = len(shards)
427426
}
428427
}
429428
// Not all shards are allocated so fallback to default shard allocation mechanisms
@@ -434,7 +433,8 @@ func (w *Worker) rebalance() error {
434433

435434
// Steal a random shard from the worker with the most shards
436435
w.shardStealInProgress = true
437-
randIndex := rand.Intn(len(workers[workerSteal]))
436+
rnd, _ := rand.Int(rand.Reader, big.NewInt(int64(len(workers[workerSteal]))))
437+
randIndex := int(rnd.Int64())
438438
shardToSteal := workers[workerSteal][randIndex]
439439
log.Debugf("Stealing shard %s from %s", shardToSteal, workerSteal)
440440

go.mod

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,34 @@
11
module github.com/vmware/vmware-go-kcl
22

3+
go 1.17
4+
35
require (
4-
github.com/BurntSushi/toml v0.3.1 // indirect
5-
github.com/aws/aws-sdk-go v1.34.8
6-
github.com/awslabs/kinesis-aggregation/go v0.0.0-20201211133042-142dfe1d7a6d
7-
github.com/golang/protobuf v1.3.1
8-
github.com/google/uuid v1.1.1
9-
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
10-
github.com/prometheus/client_golang v0.9.3
11-
github.com/prometheus/common v0.4.1
12-
github.com/prometheus/procfs v0.0.0-20190523193104-a7aeb8df3389 // indirect
6+
github.com/aws/aws-sdk-go v1.41.7
7+
github.com/awslabs/kinesis-aggregation/go v0.0.0-20210630091500-54e17340d32f
8+
github.com/golang/protobuf v1.5.2
9+
github.com/google/uuid v1.3.0
10+
github.com/prometheus/client_golang v1.11.0
11+
github.com/prometheus/common v0.32.1
1312
github.com/rs/zerolog v1.25.0
14-
github.com/sirupsen/logrus v1.4.2
15-
github.com/stretchr/testify v1.5.1
16-
go.uber.org/atomic v1.4.0 // indirect
17-
go.uber.org/multierr v1.2.0 // indirect
18-
go.uber.org/zap v1.11.0
13+
github.com/sirupsen/logrus v1.8.1
14+
github.com/stretchr/testify v1.7.0
15+
go.uber.org/zap v1.19.1
1916
gopkg.in/natefinch/lumberjack.v2 v2.0.0
2017
)
2118

22-
go 1.13
19+
require (
20+
github.com/BurntSushi/toml v0.4.1 // indirect
21+
github.com/beorn7/perks v1.0.1 // indirect
22+
github.com/cespare/xxhash/v2 v2.1.2 // indirect
23+
github.com/davecgh/go-spew v1.1.1 // indirect
24+
github.com/jmespath/go-jmespath v0.4.0 // indirect
25+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
26+
github.com/pmezard/go-difflib v1.0.0 // indirect
27+
github.com/prometheus/client_model v0.2.0 // indirect
28+
github.com/prometheus/procfs v0.7.3 // indirect
29+
go.uber.org/atomic v1.9.0 // indirect
30+
go.uber.org/multierr v1.7.0 // indirect
31+
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac // indirect
32+
google.golang.org/protobuf v1.27.1 // indirect
33+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
34+
)

0 commit comments

Comments
 (0)