Skip to content

Commit

Permalink
Fix and simplify openTrace conflict handling
Browse files Browse the repository at this point in the history
Use the same code for the first and subsequent attempts.

Set the name from traceURI if traceName is not specified.

Fix undefined base name in retry attempts.

Signed-off-by: Patrick Tasse <patrick.tasse@ericsson.com>
  • Loading branch information
PatrickTasse committed Dec 1, 2020
1 parent 316820c commit e7b6819
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions packages/base/src/trace-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,37 +70,25 @@ export class TraceManager {
* @returns The opened trace
*/
async openTrace(traceURI: string, traceName?: string): Promise<Trace | undefined> {
const traceResponse = await this.fTspClient.openTrace(new Query({
'name': traceName,
'uri': traceURI
}));
const name = traceName ? traceName : traceURI.replace(/\/$/, '').replace(/(.*\/)?/, '');

const openedTrace = traceResponse.getModel();
if (openedTrace && traceResponse.isOk()) {
this.addTrace(openedTrace);
signalManager().emit(Signals.TRACE_OPENED, {trace: openedTrace});
return openedTrace;
} else if (openedTrace && traceResponse.getStatusCode() === 409) {
// Repost with a suffix as long as there are conflicts
const handleConflict = async function (tspClient: TspClient, tryNb: number): Promise<TspClientResponse<Trace>> {
const suffix = '(' + tryNb + ')';
return tspClient.openTrace(new Query({
'name': name + suffix,
'uri': traceURI
}));
};
let conflictResolutionResponse = traceResponse;
let i = 1;
while (conflictResolutionResponse.getStatusCode() === 409) {
conflictResolutionResponse = await handleConflict(this.fTspClient, i);
i++;
}
const trace = conflictResolutionResponse.getModel();
if (trace && conflictResolutionResponse.isOk()) {
this.addTrace(trace);
signalManager().emit(Signals.TRACE_OPENED, {trace: openedTrace});
return trace;
}
const tryOpen = async function (tspClient: TspClient, retry: number): Promise<TspClientResponse<Trace>> {
return tspClient.openTrace(new Query({
'name': retry === 0 ? name : name + '(' + retry + ')',
'uri': traceURI
}));
};
let tryNb = 0;
let traceResponse: TspClientResponse<Trace> | undefined;
while (traceResponse === undefined || traceResponse.getStatusCode() === 409) {
traceResponse = await tryOpen(this.fTspClient, tryNb);
tryNb++;
}
if (traceResponse.isOk()) {
const trace = traceResponse.getModel();
this.addTrace(trace);
signalManager().emit(Signals.TRACE_OPENED, {trace: trace});
return trace;
}
// TODO Handle trace open errors
return undefined;
Expand Down

0 comments on commit e7b6819

Please sign in to comment.