55 "github.com/miniclip/gonsul/internal/entities"
66 "github.com/miniclip/gonsul/internal/util"
77
8- "encoding/json"
98 "errors"
109 "fmt"
1110 "net/http"
@@ -41,11 +40,11 @@ func (i *importer) Start(localData map[string]string) {
4140 // Create our operations Matrix
4241 ops = i .createOperationMatrix (liveData , localData )
4342
43+ // Print operation table
44+ i .printOperations (ops , entities .OperationAll )
4445 // Check if it's a dry run
4546 if i .config .GetStrategy () == config .StrategyDry {
46- // Print matrix and exit
47- i .printOperations (ops , entities .OperationAll )
48-
47+ // Exit after having printed the operations table
4948 return
5049 }
5150
@@ -72,7 +71,11 @@ func (i *importer) processOperations(matrix entities.OperationMatrix) {
7271 util .ExitError (errors .New ("" ), util .ErrorDeleteNotAllowed , i .logger )
7372 }
7473
74+ // Initialize the batch counter
75+ batch := 1
76+
7577 var transactions []entities.ConsulTxn
78+ var newTransactions []entities.ConsulTxn
7679
7780 // Fill our channel to indicate a non interruptible work (It stops here if interruption in progress)
7881 i .config .WorkingChan () <- true
@@ -84,12 +87,6 @@ func (i *importer) processOperations(matrix entities.OperationMatrix) {
8487 verb := op .GetVerb ()
8588 path := op .GetPath ()
8689
87- currentPayload , err := json .Marshal (transactions )
88- if err != nil {
89- util .ExitError (errors .New ("Marshal: " + err .Error ()), util .ErrorFailedJsonEncode , i .logger )
90- }
91- currentPayloadSize := len (currentPayload )
92-
9390 var TxnKV entities.ConsulTxnKV
9491
9592 if op .GetType () == entities .OperationDelete {
@@ -99,32 +96,24 @@ func (i *importer) processOperations(matrix entities.OperationMatrix) {
9996 TxnKV = entities.ConsulTxnKV {Verb : & verb , Key : & path , Value : & val }
10097 }
10198
102- // If the next transaction brings us over the maximum payload size, flush the current transactions
103- nextOpPayload , err := json .Marshal (TxnKV )
104- if err != nil {
105- util .ExitError (errors .New ("Marshal: " + err .Error ()), util .ErrorFailedJsonEncode , i .logger )
106- }
99+ // add the next transaction and check payload lenght
100+ newTransactions = transactions
101+ newTransactions = append (transactions , entities.ConsulTxn {KV : TxnKV })
102+ newPayloadSize := i .getTransactionsPayloadSize (& newTransactions )
107103
108- if currentPayloadSize + len (nextOpPayload ) > maximumPayloadSize {
109- i .processConsulTransaction (transactions )
104+ if newPayloadSize > maximumPayloadSize || len (transactions ) == consulTxnLimit {
105+ i .processConsulTransaction (transactions , batch )
110106 transactions = []entities.ConsulTxn {}
107+
108+ batch ++
111109 }
112110
113111 transactions = append (transactions , entities.ConsulTxn {KV : TxnKV })
114-
115- if len (transactions ) == consulTxnLimit {
116- // Flush current transactions because we hit max operation per transaction
117- // One day Consul will release an API endpoint from where we can get this limit
118- // so we do can stop hardcoding this constant
119- i .processConsulTransaction (transactions )
120- // Reset our transaction array
121- transactions = []entities.ConsulTxn {}
122- }
123112 }
124113
125114 // Do we have transactions to process
126115 if len (transactions ) > 0 {
127- i .processConsulTransaction (transactions )
116+ i .processConsulTransaction (transactions , batch )
128117 }
129118
130119 // Consume our channel, to re-allow application interruption
0 commit comments