Skip to content

Commit 56e2876

Browse files
authored
Backport bulk helper index drift bugfix to 7.x (#1915)
1 parent a59227b commit 56e2876

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

lib/Helpers.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,11 @@ class Helpers {
705705
}
706706
const retry = []
707707
const { items } = body
708+
let indexSlice = 0
708709
for (let i = 0, len = items.length; i < len; i++) {
709710
const action = items[i]
710711
const operation = Object.keys(action)[0]
711712
const { status } = action[operation]
712-
const indexSlice = operation !== 'delete' ? i * 2 : i
713713

714714
if (status >= 400) {
715715
// 429 is the only staus code where we might want to retry
@@ -736,6 +736,7 @@ class Helpers {
736736
} else {
737737
stats.successful += 1
738738
}
739+
operation === 'delete' ? indexSlice += 1 : indexSlice += 2
739740
}
740741
callback(null, retry)
741742
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
"homepage": "http://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html",
1414
"version": "7.17.11",
15-
"versionCanary": "7.17.11-canary.0",
15+
"versionCanary": "7.17.11-canary.1",
1616
"keywords": [
1717
"elasticsearch",
1818
"elastic",

test/unit/helpers/bulk.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,70 @@ test('bulk delete', t => {
10821082
server.stop()
10831083
})
10841084

1085+
t.test('Should call onDrop on the correct document when doing a mix of operations that includes deletes', async t => {
1086+
// checks to ensure onDrop doesn't provide the wrong document when some operations are deletes
1087+
// see https://github.com/elastic/elasticsearch-js/issues/1751
1088+
async function handler (req, res) {
1089+
res.setHeader('content-type', 'application/json')
1090+
res.end(JSON.stringify({
1091+
took: 0,
1092+
errors: true,
1093+
items: [
1094+
{ delete: { status: 200 } },
1095+
{ index: { status: 429 } },
1096+
{ index: { status: 200 } }
1097+
]
1098+
}))
1099+
}
1100+
1101+
const [{ port }, server] = await buildServer(handler)
1102+
const client = new Client({ node: `http://localhost:${port}` })
1103+
let counter = 0
1104+
const result = await client.helpers.bulk({
1105+
datasource: dataset.slice(),
1106+
concurrency: 1,
1107+
wait: 10,
1108+
retries: 0,
1109+
onDocument (doc) {
1110+
counter++
1111+
if (counter === 1) {
1112+
return {
1113+
delete: {
1114+
_index: 'test',
1115+
_id: String(counter)
1116+
}
1117+
}
1118+
} else {
1119+
return {
1120+
index: {
1121+
_index: 'test'
1122+
}
1123+
}
1124+
}
1125+
},
1126+
onDrop (doc) {
1127+
t.same(doc, {
1128+
status: 429,
1129+
error: null,
1130+
operation: { index: { _index: 'test' } },
1131+
document: { user: 'arya', age: 18 },
1132+
retried: false
1133+
})
1134+
}
1135+
})
1136+
1137+
t.type(result.time, 'number')
1138+
t.type(result.bytes, 'number')
1139+
t.match(result, {
1140+
total: 3,
1141+
successful: 2,
1142+
retry: 0,
1143+
failed: 1,
1144+
aborted: false
1145+
})
1146+
server.stop()
1147+
})
1148+
10851149
t.end()
10861150
})
10871151

0 commit comments

Comments
 (0)