Golang dynamodb并行扫描对于所有段返回相同的LastEvaluatedKey。

huangapple go评论80阅读模式
英文:

Golang dynamodb parallel scan returning same LastEvaluatedKey for all segment

问题

我正在运行一个使用golang编写的程序,它对dynamodb表进行并行扫描。我的ScanInput对象如下所示:

params = &dynamodb.ScanInput{
    TableName:         aws.String(tableName),
    ExclusiveStartKey: lastEvalKey,
    Segment:           aws.Int64(segment),
    TotalSegments:     aws.Int64(TOTAL_SEG),
}

除了第一个segment之外,我对所有的segment都遇到了错误。例如,如果totalsegment为4,那么第3个segment将返回以下错误:

ValidationException: The provided starting key is invalid: Invalid ExclusiveStartKey. Please use ExclusiveStartKey with correct Segment. TotalSegments: 4 Segment: 2

通过调试,我检查到出现错误的segment的LastEvaluatedKey与没有抛出错误的segment相同。

我将扫描函数作为goroutine在waitgroup中调用,每个segment一个goroutine。

var wg sync.WaitGroup
wg.Add(int(TOTAL_SEG))

for i := 0; i < int(TOTAL_SEG); i++ {
    go func(i int) {
        Scan(int64(i))
        wg.Done()
    }(i)
}

wg.Wait()

有人可以指导一下问题出在哪里吗?提前感谢。

英文:

I am running a golang program that do parallel scan of dynamodb table. My ScanInput object is:

params = &amp;dynamodb.ScanInput{
				TableName:         aws.String(tableName),
				ExclusiveStartKey: lastEvalKey,
				Segment:       aws.Int64(segment),
				TotalSegments: aws.Int64(TOTAL_SEG),
			}

I am getting error for all segment except first. For example if totalsegment is 4 than 3 will return below error:

ValidationException: The provided starting key is invalid: Invalid ExclusiveStartKey. Please use ExclusiveStartKey with correct Segment. TotalSegments: 4 Segment: 2

I checked by debugging that for segment that is having error LastEvaluatedKey is same as segment that is not throwing error.

I am calling the function that do scan as goroutine in waitgroup for each segment.

var wg sync.WaitGroup
	wg.Add(int(TOTAL_SEG))

	for i := 0; i &lt; int(TOTAL_SEG); i++ {
		go func(i int) {
			Scan(int64(i))
			wg.Done()
		}(i)
	}

	wg.Wait()

Can anyone guide what is issue. Thanks in advance.

答案1

得分: 1

解决了这个问题。问题是在第一次扫描的每个段落中,我将ExclusiveStartKey设置为nil,导致每个段落的起始点相同,并返回相同的LastEvaluatedKey值。

对于第一次扫描,我没有为每个段落传递ExclusiveStartKey,而对于后续的每个段落扫描,我使用了前一次扫描中返回的LastEvaluatedKey作为ExclusiveStartKey

在仔细检查了https://amazon-dynamodb-labs.com/design-patterns/ex2scan/step2.html上的示例后,我解决了这个问题。

英文:

Solve the issue. Problem was for every segment for first scan I was passing ExclusiveStartKey as nil due to which starting point was same for every segment and it was returning same value of LastEvaluatedKey.

For first scan I did not pass ExclusiveStartKey for each segment and for subsequent scan for each segment I used LastEvaluatedKey return in previous scan in ExclusiveStartKey.

After carefully checking example at https://amazon-dynamodb-labs.com/design-patterns/ex2scan/step2.html I solve the issue.

huangapple
  • 本文由 发表于 2021年10月20日 19:11:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/69644943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定