@@ -7,17 +7,11 @@ const config = require('config')
7
7
const elasticsearch = require ( 'elasticsearch' )
8
8
const _ = require ( 'lodash' )
9
9
const Joi = require ( '@hapi/joi' )
10
- const { Mutex } = require ( 'async-mutex' )
11
- const logger = require ( './logger' )
12
10
13
11
AWS . config . region = config . ES . AWS_REGION
14
12
15
13
// Elasticsearch client
16
14
let esClient
17
- let transactionId
18
- // Mutex to ensure that only one elasticsearch action is carried out at any given time
19
- const esClientMutex = new Mutex ( )
20
- const mutexReleaseMap = { }
21
15
22
16
/**
23
17
* Get Kafka options
@@ -57,31 +51,6 @@ async function getESClient () {
57
51
host
58
52
} )
59
53
}
60
-
61
- // Patch the transport to enable mutex
62
- esClient . transport . originalRequest = esClient . transport . request
63
- esClient . transport . request = async ( params ) => {
64
- const tId = _ . get ( params . query , 'transactionId' )
65
- params . query = _ . omit ( params . query , 'transactionId' )
66
- if ( ! tId || tId !== transactionId ) {
67
- const release = await esClientMutex . acquire ( )
68
- mutexReleaseMap [ tId || 'noTransaction' ] = release
69
- transactionId = tId
70
- }
71
- try {
72
- return await esClient . transport . originalRequest ( params )
73
- } finally {
74
- if ( params . method !== 'GET' || ! tId ) {
75
- const release = mutexReleaseMap [ tId || 'noTransaction' ]
76
- delete mutexReleaseMap [ tId || 'noTransaction' ]
77
- transactionId = undefined
78
- if ( release ) {
79
- release ( )
80
- }
81
- }
82
- }
83
- }
84
-
85
54
return esClient
86
55
}
87
56
@@ -102,66 +71,50 @@ function validProperties (payload, keys) {
102
71
/**
103
72
* Function to get user from es
104
73
* @param {String } userId
105
- * @param {String } transactionId
106
74
* @returns {Object } user
107
75
*/
108
- async function getUser ( userId , transactionId ) {
76
+ async function getUser ( userId ) {
109
77
const client = await getESClient ( )
110
- const user = await client . get ( { index : config . get ( 'ES.USER_INDEX' ) , type : config . get ( 'ES.USER_TYPE' ) , id : userId , transactionId } )
111
- return { seqNo : user . _seq_no , primaryTerm : user . _primary_term , user : user . _source }
78
+ return client . getSource ( { index : config . get ( 'ES.USER_INDEX' ) , type : config . get ( 'ES.USER_TYPE' ) , id : userId } )
112
79
}
113
80
114
81
/**
115
82
* Function to update es user
116
83
* @param {String } userId
117
- * @param {Number } seqNo
118
- * @param {Number } primaryTerm
119
- * @param {String } transactionId
120
84
* @param {Object } body
121
85
*/
122
- async function updateUser ( userId , body , seqNo , primaryTerm , transactionId ) {
86
+ async function updateUser ( userId , body ) {
123
87
const client = await getESClient ( )
124
88
await client . update ( {
125
89
index : config . get ( 'ES.USER_INDEX' ) ,
126
90
type : config . get ( 'ES.USER_TYPE' ) ,
127
91
id : userId ,
128
- transactionId,
129
- body : { doc : body } ,
130
- if_seq_no : seqNo ,
131
- if_primary_term : primaryTerm
92
+ body : { doc : body }
132
93
} )
133
94
}
134
95
135
96
/**
136
97
* Function to get org from es
137
98
* @param {String } organizationId
138
- * @param {String } transactionId
139
99
* @returns {Object } organization
140
100
*/
141
- async function getOrg ( organizationId , transactionId ) {
101
+ async function getOrg ( organizationId ) {
142
102
const client = await getESClient ( )
143
- const org = await client . get ( { index : config . get ( 'ES.ORGANIZATION_INDEX' ) , type : config . get ( 'ES.ORGANIZATION_TYPE' ) , id : organizationId , transactionId } )
144
- return { seqNo : org . _seq_no , primaryTerm : org . _primary_term , org : org . _source }
103
+ return client . getSource ( { index : config . get ( 'ES.ORGANIZATION_INDEX' ) , type : config . get ( 'ES.ORGANIZATION_TYPE' ) , id : organizationId } )
145
104
}
146
105
147
106
/**
148
107
* Function to update es organization
149
108
* @param {String } organizationId
150
- * @param {Number } seqNo
151
- * @param {Number } primaryTerm
152
- * @param {String } transactionId
153
109
* @param {Object } body
154
110
*/
155
- async function updateOrg ( organizationId , body , seqNo , primaryTerm , transactionId ) {
111
+ async function updateOrg ( organizationId , body ) {
156
112
const client = await getESClient ( )
157
113
await client . update ( {
158
114
index : config . get ( 'ES.ORGANIZATION_INDEX' ) ,
159
115
type : config . get ( 'ES.ORGANIZATION_TYPE' ) ,
160
116
id : organizationId ,
161
- transactionId,
162
- body : { doc : body } ,
163
- if_seq_no : seqNo ,
164
- if_primary_term : primaryTerm
117
+ body : { doc : body }
165
118
} )
166
119
}
167
120
@@ -177,21 +130,6 @@ function getErrorWithStatus (message, statusCode) {
177
130
return error
178
131
}
179
132
180
- /**
181
- * Ensure the esClient mutex is released
182
- * @param {String } tId transactionId
183
- */
184
- function checkEsMutexRelease ( tId ) {
185
- if ( tId === transactionId ) {
186
- const release = mutexReleaseMap [ tId ]
187
- delete mutexReleaseMap [ tId ]
188
- transactionId = undefined
189
- if ( release ) {
190
- release ( )
191
- }
192
- }
193
- }
194
-
195
133
module . exports = {
196
134
getKafkaOptions,
197
135
getESClient,
@@ -200,6 +138,5 @@ module.exports = {
200
138
updateUser,
201
139
getOrg,
202
140
updateOrg,
203
- getErrorWithStatus,
204
- checkEsMutexRelease
141
+ getErrorWithStatus
205
142
}
0 commit comments