-
Notifications
You must be signed in to change notification settings - Fork 12
/
ops.dos
399 lines (346 loc) · 14.8 KB
/
ops.dos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
module ops
/* *
* @ brief Cancel background jobs on any node in the cluster.
* If id is not specified, all running background jobs in the cluster will be canceled.
* If id is specified, running background jobs whose job IDs contain id will be canceled.
* @ param
* id indicates the job ID. It can be obtained by function getRecentJobs().
* @ Return NULL
* @ sample usage cancelJobEx("myjob1")
*/
def cancelJobEx(id=NULL){
if (id == NULL){// delete all jobs in the cluster
ids = exec jobId from pnodeRun(getRecentJobs) where endTime = NULL
}else{// delete job by JobID.
ids = exec jobId from pnodeRun(getRecentJobs) where endTime = NULL and jobId = id
}
pnodeRun(cancelJob{ids})
}
/* *
* @ brief Close inactive sessions.
* @ param
* hours determines the interval (in hours) after which a session is determined as inactive. The default value is 12.
* @ Return a table of the active connections
* @ sample usage closeInactiveSessions(24)
*/
def closeInactiveSessions(hours=12) {
if(isNull(hours))
throw "Please input the interval (in hours)"
sessionIds = exec sessionId from pnodeRun(getSessionMemoryStat) where now() - localtime(lastActiveTime) > long(hours*60*60*1000)
pnodeRun(closeSessions{sessionIds})
return pnodeRun(getSessionMemoryStat)
}
/* *
* @brief Print the Data Definition Language (DDL) statement of a DFS table.
* @param
* database is the absolute path of the folder where the database is stored
* tableName is the name of dfs table
* @return print the sql statement of creating a DFS table
* @sample usage getDDL("dfs://demodb", "pt")
*
*/
def getDDL(database, tableName){
// Prompt if the table does not exist
if(!existsTable(database, tableName)){
return "Please check if the table exists"
}
// get the definition of columns
tSchema = schema(loadTable(database, tableName))
colName = tSchema.colDefs.name
colType = tSchema.colDefs.typeString
colName = "`"+concat(colName, "`")
colType = "["+concat(colType, ",")+"]"
// get the column name of partition
partitionCol = tSchema.partitionColumnName
if(size(partitionCol) > 1)
partitionCol = "`"+concat(partitionCol, "`")
else
partitionCol = "`"+string(partitionCol)
// todo compressMethods
// sortColumns, keepDuplicates, sortKeyMappingFunction
if(tSchema.engineType ==`TSDB){
sortColumns = tSchema.sortColumns
if(size(sortColumns) > 1)
sortColumns = "`"+concat(sortColumns, "`")
else
sortColumns = "`"+string(sortColumns)
keepDuplicates = tSchema.keepDuplicates
// todo sortKeyMappingFunction
}
// print sql of creating table
print("db = database("+'\"'+database+'\")')
print("colName = " + colName)
print("colType = " + colType)
print("tbSchema = table(1:0, colName, colType)")
if(tSchema.engineType !=`TSDB){
if(tSchema.partitionColumnIndex.size()==1) {
if(tSchema.partitionColumnIndex==-1)
print("db.createTable(table=tbSchema,tableName=`" +tableName+")")
else
print("db.createPartitionedTable(table=tbSchema,tableName=`" +tableName+",partitionColumns="+partitionCol+")")
}else{
print("db.createPartitionedTable(table=tbSchema,tableName=`" +tableName+",partitionColumns="+partitionCol+")")
}
} else{
if(tSchema.partitionColumnIndex.size()==1 ){
if(tSchema.partitionColumnIndex==-1)
print("db.createTable(table=tbSchema,tableName=`"+tableName+",sortColumns=" +sortColumns+",keepDuplicates="+keepDuplicates+")")
else
print("db.createPartitionedTable(table=tbSchema,tableName=`"+tableName+",partitionColumns=" +partitionCol + ",sortColumns="+sortColumns+",keepDuplicates="+keepDuplicates+")")
}else{
print("db.createPartitionedTable(table=tbSchema,tableName=`"+tableName+",partitionColumns=" +partitionCol + ",sortColumns="+sortColumns+",keepDuplicates="+keepDuplicates+")")
}
}
}
/* *
* @ brief Get the disk space occupied by the DFS table
* @ param
* database is the absolute path of the folder where the database is stored
* tableName is the name of dfs table
* byNode is a Boolean value, indicating whether the disk usage is displayed by node
* @ Return a table containing the disk space occupied by the DFS table
* @ sample usage getTableDiskUsage("dfs://demodb", "machines", true)
*/
def getTableDiskUsage(database, tableName, byNode=false){
if(byNode == true){
return select sum(diskUsage\1024\1024\1024) diskGB
from pnodeRun(getTabletsMeta{"/"+substr(database, 6)+"/%", tableName, true, -1})
group by node
}else {
return select sum(diskUsage\1024\1024\1024) diskGB
from pnodeRun(getTabletsMeta{"/"+substr(database, 6)+"/%", tableName, true, -1})
}
}
/* *
* @ brief Force delete the recovering partition.
* @ param
* dbPath is the absolute path of the folder where the database is stored
* tableName is the name of dfs table, required if chunkGranularity is TABLE
* @ return NULL
* @ sample usage dropRecoveringPartitions("dfs://compoDB")
*/
def dropRecoveringPartitions(dbPath , tableName=""){
db=database(dbPath)
if(db.schema().chunkGranularity=="TABLE"){
if (isNull(tableName) or tableName=="")
throw "Please input the table name"
}
dbName = substr(dbPath, 5)
partitions = exec substr(file, strlen(dbName))
from rpc(getControllerAlias(), getClusterChunksStatus)
where like(file, dbName + "%"), state != "COMPLETE"
if(db.schema().chunkGranularity=="TABLE")
dropPartition(db, partitions, tableName, true)
else
dropPartition(db, partitions, , true)
}
/* *
* @ brief Get the expiration date of the license on all nodes in the cluster.
* @ param NULL
* @ return a table containing expiration date of the license on each node
* @ sample usage getAllLicenses()
*/
def getAllLicenses(){
t = select node as nodeAlias,value as endDate from pnodeRun(getLicenseExpiration)
nodes = exec name, host, port from rpc(getControllerAlias(), getClusterPerf{true}) where not mode in [0, 3, 4]
for(node in nodes){
try{
conn = xdb(node[`host], node[`port])
t1 = remoteRun(
conn, "table( getNodeAlias() as node ,getLicenseExpiration() as date)")
t.append!(t1)
} catch(ex){
print(ex)
writeLog(ex)
}
}
return t
}
/* *
* @ brief Update the license on all nodes in the cluster.
* @ param NULL
* @ return a table containing expiration date of the license on each node
* @ sample usage updateAllLicenses(), Must be done after replacing the license file
*/
def updateAllLicenses(){
try{
pnodeRun(getLicenseExpiration)
}catch(ex){
print(ex)
writeLog(ex)
}
nodes = exec name, host, port from rpc(getControllerAlias(), getClusterPerf{true}) where not mode in [0, 3, 4]
for(node in nodes){
try{
conn = xdb(node[`host], node[`port])
conn(updateLicense())
} catch(ex){
print(ex)
writeLog(ex)
}
}
return getAllLicenses()
}
/* *
* @ brief Cancel all subscription on the node
* @ param NULL
* @ return NULL
*/
def unsubscribeAll() {
t = getStreamingStat().pubTables
for(row in t){
tableName = row.tableName
if(string(row.actions).startsWith("[")) {
actions = split(substr(row.actions, 1, strlen(row.actions)-2), ",")
} else {
actions = [].append!(row.actions)
}
for(action in actions){
unsubscribeTable(tableName=tableName, actionName=action)
}
}
}
/**
* @ brief Get the performance measures of the cluster within the given monitoring period and interval, save the result to a CSV file.
* @ param
* monitoringPeriod indicates the monitoring period in seconds, default is 60.
* scrapeInterval is the scrape interval in seconds, default is 15
* dir is the directory to save the CSV file,default is /tmp.
* @ return NULL
*/
def gatherClusterPerf(monitoringPeriod=60, scrapeInterval=15, dir="/tmp"){
targetDir = dir
if(targetDir == "" || targetDir == string(NULL)){
targetDir = "/tmp"
}
schema0 = schema(rpc(getControllerAlias(), getClusterPerf))
colNames = [`ts]
colNames.append!(schema0[`colDefs][`name])
colTypes = [TIMESTAMP]
colTypes.append!(schema0[`colDefs][`typeInt])
statis_table = table(100000: 0, colNames, colTypes)
startTime = now()
do {
insert into statis_table select now() as ts, * from rpc(getControllerAlias(), getClusterPerf)
sleep(scrapeInterval * 1000)
elasped_time = (now() - startTime)/1000
}while(elasped_time < monitoringPeriod)
saveText(statis_table, targetDir + "/statis.csv")
}
/**
* @ brief Get the status of workers of the subscriber nodes within the given monitoring period and interval, save the result to a CSV file.
* @ param:
* subNode, the nodeAlias of subscribe node
* monitoringPeriod indicates the monitoring period in seconds,default is 60.
* scrapeInterval is the scrape interval in seconds, default is 15
* dir is the directory to save csv file,default is /tmp.
* @ return NULL
*/
def gatherStreamingStat(subNode, monitoringPeriod=60, scrapeInterval=15, dir="/tmp"){
targetDir = dir
if(targetDir == "" || targetDir == string(NULL)){
targetDir = "/tmp"
}
schema0 = schema(rpc(subNode, getStreamingStat).subWorkers)
colNames = [`ts]
colNames.append!(schema0[`colDefs][`name])
colTypes = [TIMESTAMP]
colTypes.append!(schema0[`colDefs][`typeInt])
stream_statis_table = table(100000: 0, colNames, colTypes)
startTime = now()
do {
insert into stream_statis_table select now() as ts, * from rpc(subNode, getStreamingStat).subWorkers
sleep(scrapeInterval * 1000)
elasped_time = (now() - startTime)/1000
}while(elasped_time < monitoringPeriod)
saveText(stream_statis_table, targetDir + "/sub_worker_statis.csv")
}
/**
* @ brief Compares whether the data for two memory tables is the same
* @ param:
* t1, the memory tables
* t2, the memory tables
* @ return returns records with different rows of data if different,else print Both tables are identical
*/
def getDifferentData(t1, t2){
res = each(eqObj, t1.values(), t2.values())
if(res.all()!=true){
colIndex = at(res==false)
comparison = each(eqObj, t1.col(colIndex[0]), t2.col(colIndex[0]))
rowIndex = at(comparison==false)
return [t1[rowIndex], t2[rowIndex]]
}else{
print "Both tables are identical"
}
}
def checkOLAPChunkReplicas(dbName, tableName, targetChunkId){
pathTable = select path, latestPhysicalDir from pnodeRun(getTabletsMeta) where chunkId==uuid(targetChunkId), tableName==tableName
if (size(pathTable) == 1) {
throw "no replica found, no need to check"
}
symbolCols=exec name from loadTable(dbName, tableName).schema().colDefs where typeString ="SYMBOL"
if(pathTable.size()!=0){
db=database(dbName)
colFiles1=exec filename from files(pathTable["path"][0]+"/"+pathTable["latestPhysicalDir"][0]) order by filename
colFiles2=exec filename from files(pathTable["path"][1]+"/"+pathTable["latestPhysicalDir"][1]) order by filename
if(colFiles1.size()!=0 && colFiles2.size()!=0){
if(eqObj(colFiles1, colFiles2)==false){
throw "colFiles on two replicas are not same"
}else{
res=array(BOOL, 0, size(colFiles1))
for(colFile in colFiles1){
colName=split(colFile,".")[0]
if (colName in symbolCols){
res1=loadColumn(db, tableName, pathTable["path"][0]+"/"+pathTable["latestPhysicalDir"][0]+"/"+colFile,pathTable["path"][0]+"/chunk.dict")
res2=loadColumn(db, tableName, pathTable["path"][1]+"/"+pathTable["latestPhysicalDir"][1]+"/"+colFile,pathTable["path"][1]+"/chunk.dict")
}else{
res1=loadColumn(db, tableName, pathTable["path"][0]+"/"+pathTable["latestPhysicalDir"][0]+"/"+colFile)
res2=loadColumn(db, tableName, pathTable["path"][1]+"/"+pathTable["latestPhysicalDir"][1]+"/"+colFile)
}
comparison=eqObj(res1, res2)
res.append!(comparison)
}
}
if(res.all()==true){
return true
}else{
return false
}
}else{
throw "colfiles is not exist"
}
}else{
throw "physicalTableDir is not exist"
}
}
def checkTSDBChunkReplicas(dbName, tableName, targetChunkId){
re = array(BOOL,0,10)
for(lvl in 0..3){
levelFileInfo = select * from pnodeRun(getTSDBMetaData) where chunkId==targetChunkId and like(table,tableName+"%") and level=lvl
if(levelFileInfo.size()!=0){
levelFilesSize = size(split(levelFileInfo[`files][0],","))-1
for(i in 0..(levelFilesSize-1)){
res1 = rpc(levelFileInfo[`node][0],getLevelFileData,levelFileInfo[`chunkPath][0]+"/"+levelFileInfo[`table][0]+"/"+split(levelFileInfo[`files][0],",")[i])
res2 = rpc(levelFileInfo[`node][1],getLevelFileData,levelFileInfo[`chunkPath][1]+"/"+levelFileInfo[`table][1]+"/"+split(levelFileInfo[`files][1],",")[i])
comparison = eqObj(res1.values(), res2.values())
re.append!(comparison)
}
}
}
if(re.all()==true)
return true
return false
}
/**
* @ brief Compares whether the data for two chunk replicas is the same
* @ param:
* dbPath is the absolute path of the folder where the database is stored
* tableName is the name of dfs table
* @ return true if the same ,false if different
*/
def checkChunkReplicas(dbPath, tableName, targetChunkId){
if(database(dbPath).schema().engineType ==`TSDB)
return checkTSDBChunkReplicas(dbPath, tableName, targetChunkId);
else
return checkOLAPChunkReplicas(dbPath, tableName, targetChunkId);
}