Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit dc3c600

Browse files
Merge pull request #12 from topcoder-platform/revert-11-transaction
Revert "Identify and combine same actions in a single mutex"
2 parents 8cf077a + 2a8b87b commit dc3c600

File tree

5 files changed

+160
-257
lines changed

5 files changed

+160
-257
lines changed

src/common/helper.js

Lines changed: 41 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ AWS.config.region = config.ES.AWS_REGION
1313

1414
// Elasticsearch client
1515
let esClient
16-
1716
// Mutex to ensure that only one elasticsearch action is carried out at any given time
1817
const esClientMutex = new Mutex()
1918

@@ -56,18 +55,18 @@ async function getESClient () {
5655
})
5756
}
5857

59-
return esClient
60-
}
61-
62-
/**
63-
* Wraps original get es client function
64-
* to control access to elasticsearch using a mutex
65-
*/
66-
async function getESClientWrapper () {
67-
const client = await getESClient()
68-
const release = await esClientMutex.acquire()
58+
// Patch the transport to enable mutex
59+
esClient.transport.originalRequest = esClient.transport.request
60+
esClient.transport.request = async (params) => {
61+
const release = await esClientMutex.acquire()
62+
try {
63+
return await esClient.transport.originalRequest(params)
64+
} finally {
65+
release()
66+
}
67+
}
6968

70-
return { client, release }
69+
return esClient
7170
}
7271

7372
/**
@@ -87,25 +86,17 @@ function validProperties (payload, keys) {
8786
/**
8887
* Function to get user from es
8988
* @param {String} userId
90-
* @param {Boolean} isTransaction Is this part of a transaction?
89+
* @param {Boolean} sourceOnly
9190
* @returns {Object} user
9291
*/
93-
async function getUser (userId, isTransaction = false) {
94-
const { client, release } = await getESClientWrapper()
95-
96-
try {
97-
const user = await client.get({ index: config.get('ES.USER_INDEX'), type: config.get('ES.USER_TYPE'), id: userId })
98-
99-
if (isTransaction) {
100-
return { seqNo: user._seq_no, primaryTerm: user._primary_term, user: user._source, release }
101-
}
92+
async function getUser (userId, sourceOnly = true) {
93+
const client = await getESClient()
10294

103-
return { seqNo: user._seq_no, primaryTerm: user._primary_term, user: user._source }
104-
} finally {
105-
if (!isTransaction) {
106-
release()
107-
}
95+
if (sourceOnly) {
96+
return client.getSource({ index: config.get('ES.USER_INDEX'), type: config.get('ES.USER_TYPE'), id: userId })
10897
}
98+
99+
return client.get({ index: config.get('ES.USER_INDEX'), type: config.get('ES.USER_TYPE'), id: userId })
109100
}
110101

111102
/**
@@ -114,92 +105,43 @@ async function getUser (userId, isTransaction = false) {
114105
* @param {Number} seqNo
115106
* @param {Number} primaryTerm
116107
* @param {Object} body
117-
* @param {Boolean} isTransaction If this is part of a transaction, it will not attempt to release
118108
*/
119-
async function updateUser (userId, body, seqNo, primaryTerm, isTransaction = false) {
120-
let client, release
121-
122-
if (isTransaction) {
123-
client = await getESClient()
124-
} else {
125-
const esClient = await getESClientWrapper()
126-
client = esClient.client
127-
release = esClient.release
128-
}
129-
130-
try {
131-
await client.update({
132-
index: config.get('ES.USER_INDEX'),
133-
type: config.get('ES.USER_TYPE'),
134-
id: userId,
135-
body: { doc: body },
136-
if_seq_no: seqNo,
137-
if_primary_term: primaryTerm
138-
})
139-
} finally {
140-
if (!isTransaction) {
141-
release()
142-
}
143-
}
109+
async function updateUser (userId, body, seqNo, primaryTerm) {
110+
const client = await getESClient()
111+
await client.update({
112+
index: config.get('ES.USER_INDEX'),
113+
type: config.get('ES.USER_TYPE'),
114+
id: userId,
115+
body: { doc: body },
116+
if_seq_no: seqNo,
117+
if_primary_term: primaryTerm
118+
})
144119
}
145120

146121
/**
147122
* Function to get org from es
148123
* @param {String} organizationId
149-
* @param {Boolean} isTransaction Is this part of a transaction?
150124
* @returns {Object} organization
151125
*/
152-
async function getOrg (organizationId, isTransaction = false) {
153-
const { client, release } = await getESClientWrapper()
154-
155-
try {
156-
const org = await client.get({ index: config.get('ES.ORGANIZATION_INDEX'), type: config.get('ES.ORGANIZATION_TYPE'), id: organizationId })
157-
158-
if (isTransaction) {
159-
return { seqNo: org._seq_no, primaryTerm: org._primary_term, org: org._source, release }
160-
}
161-
162-
return { seqNo: org._seq_no, primaryTerm: org._primary_term, org: org._source }
163-
} finally {
164-
if (!isTransaction) {
165-
release()
166-
}
167-
}
126+
async function getOrg (organizationId) {
127+
const client = await getESClient()
128+
return client.getSource({ index: config.get('ES.ORGANIZATION_INDEX'), type: config.get('ES.ORGANIZATION_TYPE'), id: organizationId })
168129
}
169130

170131
/**
171132
* Function to update es organization
172133
* @param {String} organizationId
173134
* @param {Object} body
174-
* @param {Number} seqNo
175-
* @param {Number} primaryTerm
176-
* @param {Boolean} isTransaction If this is part of a transaction, it will not attempt to lock
177135
*/
178-
async function updateOrg (organizationId, body, seqNo, primaryTerm, isTransaction = false) {
179-
let client, release
180-
181-
if (isTransaction) {
182-
client = await getESClient()
183-
} else {
184-
const esClient = await getESClientWrapper()
185-
client = esClient.client
186-
release = esClient.release
187-
}
188-
189-
try {
190-
await client.update({
191-
index: config.get('ES.ORGANIZATION_INDEX'),
192-
type: config.get('ES.ORGANIZATION_TYPE'),
193-
id: organizationId,
194-
body: { doc: body },
195-
if_seq_no: seqNo,
196-
if_primary_term: primaryTerm
197-
})
198-
} finally {
199-
if (!isTransaction) {
200-
release()
201-
}
202-
}
136+
async function updateOrg (organizationId, body) {
137+
const client = await getESClient()
138+
await client.update({
139+
index: config.get('ES.ORGANIZATION_INDEX'),
140+
type: config.get('ES.ORGANIZATION_TYPE'),
141+
id: organizationId,
142+
body: { doc: body },
143+
refresh: 'true'
144+
})
203145
}
204146

205147
/**
@@ -216,7 +158,7 @@ function getErrorWithStatus (message, statusCode) {
216158

217159
module.exports = {
218160
getKafkaOptions,
219-
getESClientWrapper,
161+
getESClient,
220162
validProperties,
221163
getUser,
222164
updateUser,

0 commit comments

Comments
 (0)