From 64758086777db9a5baf56d64050af3620e3e2f5d Mon Sep 17 00:00:00 2001 From: Nakata Date: Thu, 29 Jun 2023 15:31:44 +0900 Subject: [PATCH] Fix duplicate add request --- .../libc_to_manage_container/mock_test.go | 9 +- .../libc_to_manage_container/process_data.go | 5 ++ .../process_data_test.go | 83 ++++++++++++++++--- 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/packages/server/manage_container/server/libc_to_manage_container/mock_test.go b/packages/server/manage_container/server/libc_to_manage_container/mock_test.go index c807175e5..71bfb527c 100644 --- a/packages/server/manage_container/server/libc_to_manage_container/mock_test.go +++ b/packages/server/manage_container/server/libc_to_manage_container/mock_test.go @@ -3,6 +3,7 @@ package l2mserver import ( "encoding/json" + "errors" "fmt" "sync" @@ -29,7 +30,10 @@ func (localDb) GetSharePieceSize(dataID string) (int32, error) { } mu.Lock() defer mu.Unlock() - mp, _ := db[dataID] + mp, ok := db[dataID] + if !ok { + return 0, fmt.Errorf("データ未登録エラー: %sは登録されていません.", dataID) + } return int32(len(mp)), nil } func (localDb) InsertShares(dataID string, schema []*pb_types.Schema, pieceID int32, shares string, sentAt string, matchingColumn int32) error { @@ -54,6 +58,9 @@ func (localDb) InsertShares(dataID string, schema []*pb_types.Schema, pieceID in if !ok { db[dataID] = map[int32]m2db.Share{} } + if _, ok := db[dataID][pieceID]; ok { + return errors.New("重複データ登録エラー: " + dataID + "は既に登録されています.") + } db[dataID][pieceID] = share return nil } diff --git a/packages/server/manage_container/server/libc_to_manage_container/process_data.go b/packages/server/manage_container/server/libc_to_manage_container/process_data.go index fd2101d72..b23920849 100644 --- a/packages/server/manage_container/server/libc_to_manage_container/process_data.go +++ b/packages/server/manage_container/server/libc_to_manage_container/process_data.go @@ -29,6 +29,11 @@ func (p processer) addShareDataFrame(baseDataID string, addDataID string) (strin s := fmt.Sprintf("%s+%s", baseDataID, addDataID) r := sha256.Sum256([]byte(s)) dataID := hex.EncodeToString(r[:]) + _, err := p.m2dbclient.GetSharePieceSize(dataID) + if err == nil { + // errがない(既に登録されている)場合は既に加算されてるはずなのでそのままreturn + return dataID, nil + } // pieceのindexと今見ているpieceデータのindex basePieceID := int32(0) diff --git a/packages/server/manage_container/server/libc_to_manage_container/process_data_test.go b/packages/server/manage_container/server/libc_to_manage_container/process_data_test.go index 2118077dd..65e4182f6 100644 --- a/packages/server/manage_container/server/libc_to_manage_container/process_data_test.go +++ b/packages/server/manage_container/server/libc_to_manage_container/process_data_test.go @@ -26,7 +26,7 @@ func TestAddShareDataFrameSave(t *testing.T) { {"13.00000000000000000000000000000000000000000000000000", "104.00000000000000000000000000000000000000000000000000"}, }, - DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215", + DataID: "8bf179ec763fd05e429a2ae5847cb47c2a116709e5f5df9b3c9256b278cefe51", Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, }, } @@ -37,8 +37,8 @@ func TestAddShareDataFrameSave(t *testing.T) { t.Parallel() // テスト用データをテスト用DBに保存しておく - dataID1 := fmt.Sprintf("%s%d", name, 1) - dataID2 := fmt.Sprintf("%s%d", name, 2) + dataID1 := fmt.Sprintf("TestAddShareDataFrameSave%s%d", name, 1) + dataID2 := fmt.Sprintf("TestAddShareDataFrameSave%s%d", name, 2) mu.Lock() db[dataID1] = map[int32]m2db.Share{} db[dataID1][0] = tt.baseData @@ -85,17 +85,17 @@ func TestAddPieceShareDataFrameSave(t *testing.T) { {Value: [][]string{{ "11.00000000000000000000000000000000000000000000000000", "102.00000000000000000000000000000000000000000000000000"}}, - DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215", + DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed", Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, {Value: [][]string{{ "13.00000000000000000000000000000000000000000000000000", "104.00000000000000000000000000000000000000000000000000"}}, - DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215", + DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed", Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}}, {Value: [][]string{{ "15.00000000000000000000000000000000000000000000000000", "106.00000000000000000000000000000000000000000000000000"}}, - DataID: "6a8787d0da13f1dc67fce89cbb217aeeb5473fb1dce55e846ab65f35135dd215", + DataID: "197425564f8a14fcbbf165a88daa75afc83d8ffb4a2085610b08282349d936ed", Meta: m2db.ShareMeta{PieceID: 2, MatchingColumn: 1}}, }, }, @@ -113,17 +113,17 @@ func TestAddPieceShareDataFrameSave(t *testing.T) { {Value: [][]string{{ "11.00000000000000000000000000000000000000000000000000", "102.00000000000000000000000000000000000000000000000000"}}, - DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80", + DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411", Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, {Value: [][]string{{ "13.00000000000000000000000000000000000000000000000000", "104.00000000000000000000000000000000000000000000000000"}}, - DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80", + DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411", Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}}, {Value: [][]string{{ "15.00000000000000000000000000000000000000000000000000", "106.00000000000000000000000000000000000000000000000000"}}, - DataID: "bbcc21770ff7bf4b711243017e94d8493437d92a8152aa7da7b0be276096fc80", + DataID: "7817e5c7355dd7439e28d3af0d7720f128d320f1c9fe6a3837522fff87f68411", Meta: m2db.ShareMeta{PieceID: 2, MatchingColumn: 1}}, }, }, @@ -141,14 +141,14 @@ func TestAddPieceShareDataFrameSave(t *testing.T) { {Value: [][]string{{ "11.00000000000000000000000000000000000000000000000000", "102.00000000000000000000000000000000000000000000000000"}}, - DataID: "184642e3b73a954582fb59d6feb3f0a95a458923bc3ef033059f4b64e6032cc1", + DataID: "083ee381e2733c9a6147ea050b2d6e79b494e61b34c046b4dc110b7a40d8aa91", Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, {Value: [][]string{ {"13.00000000000000000000000000000000000000000000000000", "104.00000000000000000000000000000000000000000000000000"}, {"15.00000000000000000000000000000000000000000000000000", "106.00000000000000000000000000000000000000000000000000"}}, - DataID: "184642e3b73a954582fb59d6feb3f0a95a458923bc3ef033059f4b64e6032cc1", + DataID: "083ee381e2733c9a6147ea050b2d6e79b494e61b34c046b4dc110b7a40d8aa91", Meta: m2db.ShareMeta{PieceID: 1, MatchingColumn: 1}}, }, }, @@ -160,8 +160,8 @@ func TestAddPieceShareDataFrameSave(t *testing.T) { t.Parallel() // テスト用データを1行ずつテスト用DBに保存しておく - baseDataID := fmt.Sprintf("%s%d", name, 1) - addDataID := fmt.Sprintf("%s%d", name, 2) + baseDataID := fmt.Sprintf("TestAddPieceShareDataFrameSave%s%d", name, 1) + addDataID := fmt.Sprintf("TestAddPieceShareDataFrameSave%s%d", name, 2) mu.Lock() db[baseDataID] = map[int32]m2db.Share{} for pieceID, share := range tt.baseData { @@ -191,3 +191,60 @@ func TestAddPieceShareDataFrameSave(t *testing.T) { }) } } + +// 同じIDを指定して加算したテーブルが正しく保存されてるかテスト +func TestAddShareDataFrameSaveTwice(t *testing.T) { + testcases := map[string]struct { + baseData m2db.Share + addData m2db.Share + expected m2db.Share + }{ + "NormalCase": { + baseData: m2db.Share{Value: [][]string{{"1", "2"}, {"3", "4"}}, + Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, + addData: m2db.Share{Value: [][]string{{"10", "100"}, {"10", "100"}}, + Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, + expected: m2db.Share{Value: [][]string{ + {"11.00000000000000000000000000000000000000000000000000", + "102.00000000000000000000000000000000000000000000000000"}, + {"13.00000000000000000000000000000000000000000000000000", + "104.00000000000000000000000000000000000000000000000000"}, + }, + DataID: "8508368950a44ff61bcf86db613fcf9bc2c4b409d039dec6efcfc0a25765be5c", + Meta: m2db.ShareMeta{PieceID: 0, MatchingColumn: 1}}, + }, + } + for name, tt := range testcases { + name := name + tt := tt + t.Run(name, func(t *testing.T) { + t.Parallel() + + // テスト用データをテスト用DBに保存しておく + dataID1 := fmt.Sprintf("TestAddShareDataFrameSaveTwice%s%d", name, 1) + dataID2 := fmt.Sprintf("TestAddShareDataFrameSaveTwice%s%d", name, 2) + mu.Lock() + db[dataID1] = map[int32]m2db.Share{} + db[dataID1][0] = tt.baseData + db[dataID2] = map[int32]m2db.Share{} + db[dataID2][0] = tt.addData + mu.Unlock() + + // 何回も加算処理をしてDBに加算された値が保存されているか確認 + for i := 0; i <= 5; i++ { + p := processer{m2dbclient: localDb{}} + dataID, errAdd := p.addShareDataFrame(dataID1, dataID2) + if errAdd != nil { + t.Fatal(errAdd) + } + share, errShare := p.m2dbclient.GetSharePiece(dataID, 0) + if errShare != nil { + t.Fatal(errShare) + } + if !reflect.DeepEqual(share, tt.expected) { + t.Fatalf("share must be %v, but %v", tt.expected, share) + } + } + }) + } +}