Suppose I have the following document in ElasticSearch:
<code>{
"version": 1,
"meal": "breakfast"
}
</code>
<code>{
"version": 1,
"meal": "breakfast"
}
</code>
{
"version": 1,
"meal": "breakfast"
}
Suppose I run the following code to attempt 2 simultaneous conditional updates to the document, and no error is thrown:
<code> async function retryScriptOnConflict(source) {
while (true) {
try {
await openSearchClient.update({
index: 'testindex',
id: 12345,
body: {
script: {
lang: "painless",
source: source
}
}
});
return;
} catch (error) {
if (error.meta && error.meta.body && error.meta.body.error.type === 'version_conflict_engine_exception') {
continue;
} else {
throw err;
}
}
}
}
const promise2 = retryScriptOnConflict('if (ctx._source.version < 3) {ctx._source.meal = "dinner"; ctx._source.version = 3}');
const promise1 = retryScriptOnConflict('if (ctx._source.version < 2) {ctx._source.meal = "lunch"; ctx._source.version = 2}');
await promise1;
await promise2;
</code>
<code> async function retryScriptOnConflict(source) {
while (true) {
try {
await openSearchClient.update({
index: 'testindex',
id: 12345,
body: {
script: {
lang: "painless",
source: source
}
}
});
return;
} catch (error) {
if (error.meta && error.meta.body && error.meta.body.error.type === 'version_conflict_engine_exception') {
continue;
} else {
throw err;
}
}
}
}
const promise2 = retryScriptOnConflict('if (ctx._source.version < 3) {ctx._source.meal = "dinner"; ctx._source.version = 3}');
const promise1 = retryScriptOnConflict('if (ctx._source.version < 2) {ctx._source.meal = "lunch"; ctx._source.version = 2}');
await promise1;
await promise2;
</code>
async function retryScriptOnConflict(source) {
while (true) {
try {
await openSearchClient.update({
index: 'testindex',
id: 12345,
body: {
script: {
lang: "painless",
source: source
}
}
});
return;
} catch (error) {
if (error.meta && error.meta.body && error.meta.body.error.type === 'version_conflict_engine_exception') {
continue;
} else {
throw err;
}
}
}
}
const promise2 = retryScriptOnConflict('if (ctx._source.version < 3) {ctx._source.meal = "dinner"; ctx._source.version = 3}');
const promise1 = retryScriptOnConflict('if (ctx._source.version < 2) {ctx._source.meal = "lunch"; ctx._source.version = 2}');
await promise1;
await promise2;
If I check the document again an hour later, and nobody else changed it since, is it guaranteed that no matter what order the requests executed in, the document will contain:
<code>{
"version": 3,
"meal": "dinner"
}
</code>
<code>{
"version": 3,
"meal": "dinner"
}
</code>
{
"version": 3,
"meal": "dinner"
}
?
i.e. is it guaranteed that the right update wins?