Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not use exceptions for deregistration errors #1303

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions common/result-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ const EventHandlerErrorCode = {
MISSING_CONFIG_IN_MSG_DATA: 70007,
DUPLICATED_GLOBAL_FILTER_ID: 70008,
INVALID_EVENT_TYPE_IN_VALIDATE_FUNC: 70009,
NO_MATCHED_FILTERS: 70010,
MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS: 70011,
PARSING_GLOBAL_FILTER_ID_FAILURE: 70012,
NO_MATCHED_FILTERS: 70010, // Deprecated (2024-08-14).
MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS: 70011, // Deprecated (2024-08-14)
PARSING_GLOBAL_FILTER_ID_FAILURE: 70012, // Deprecated (2024-08-14).
DUPLICATED_CHANNEL_ID: 70013,
EVENT_CHANNEL_EXCEEDS_SIZE_LIMIT: 70014,
EVENT_FILTER_EXCEEDS_SIZE_LIMIT: 70015,
EVENT_FILTER_EXCEEDS_SIZE_LIMIT_PER_CHANNEL: 70016,
FAILED_TO_REGISTER_FILTER: 70020,
FAILED_TO_DEREGISTER_FILTER: 70030,
FAILED_TO_DEREGISTER_FILTER: 70030, // Deprecated (2024-08-14).
INVALID_CUSTOM_CLIENT_ID: 70040,
// BLOCK_FINALIZED (701XX)
NEGATIVE_BLOCK_NUMBER: 70100,
Expand All @@ -234,9 +234,9 @@ const EventHandlerErrorCode = {
MISSING_PATH_IN_CONFIG: 70200,
INVALID_FORMAT_PATH: 70201,
// VALUE_CHANGED & StateEventTreeManager (7025X)
MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH: 70250,
MISSING_FILTER_ID_SET: 70251,
MISSING_FILTER_ID_IN_FILTER_ID_SET: 70252,
MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH: 70250, // Deprecated (2024-08-14).
MISSING_FILTER_ID_SET: 70251, // Deprecated (2024-08-14).
MISSING_FILTER_ID_IN_FILTER_ID_SET: 70252, // Deprecated (2024-08-14).
// TX_STATE_CHANGED (703XX)
MISSING_TX_HASH_IN_CONFIG: 70300,
INVALID_TX_HASH: 70301,
Expand Down
43 changes: 17 additions & 26 deletions event-handler/event-channel-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,25 +236,15 @@ class EventChannelManager {

deregisterFilter(channel, clientFilterId) {
const filter = this.node.eh.deregisterEventFilter(clientFilterId, channel.id);
if (!filter) {
return;
}
channel.deleteEventFilterId(filter.id);
delete this.filterIdToChannelId[filter.id];
}

deregisterFilterAndEmitEvent(channel, clientFilterId, filterDeletionReason) {
const LOG_HEADER = 'deregisterFilterAndEmitEvent';
try {
this.deregisterFilter(channel, clientFilterId);
} catch (err) {
logger.error(`[${LOG_HEADER}] Can't deregister event filter ` +
`(clientFilterId: ${clientFilterId}, channelId: ${channel.id}, ` +
`err: ${err.message} at ${err.stack})`);
throw new EventHandlerError(
EventHandlerErrorCode.FAILED_TO_DEREGISTER_FILTER,
`Failed to deregister filter with filter ID: ${clientFilterId} ` +
`due to error: ${err.message}`,
clientFilterId
);
}
this.deregisterFilter(channel, clientFilterId);
const blockchainEvent = new BlockchainEvent(
BlockchainEventTypes.FILTER_DELETED,
{
Expand Down Expand Up @@ -344,6 +334,9 @@ class EventChannelManager {
// TODO(ehgmsdk20): reuse same object for memory
const eventObj = event.toObject();
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(eventFilterId);
if (!clientFilterId) {
return;
}
Object.assign(eventObj, { filter_id: clientFilterId });
this.transmitEventObj(channel, eventObj);
}
Expand All @@ -369,20 +362,18 @@ class EventChannelManager {

closeChannel(channel) {
const LOG_HEADER = 'closeChannel';
try {
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`);
channel.webSocket.terminate();
const filterIds = channel.getAllFilterIds();
for (const filterId of filterIds) {
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(filterId);
// NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
this.deregisterFilter(channel, clientFilterId);
logger.info(`[${LOG_HEADER}] Closing channel ${channel.id}`);
channel.webSocket.terminate();
const filterIds = channel.getAllFilterIds();
for (const filterId of filterIds) {
const clientFilterId = this.node.eh.getClientFilterIdFromGlobalFilterId(filterId);
if (!clientFilterId) {
continue;
}
delete this.channels[channel.id];
} catch (err) {
logger.error(`[${LOG_HEADER}] Error while closing channel (channelId: ${channel.id}, ` +
`message:${err.message})`);
// NOTE(ehgmsdk20): Do not emit filter_deleted event because the channel is already closed.
this.deregisterFilter(channel, clientFilterId);
}
delete this.channels[channel.id];
}

startHeartbeat(wsServer) {
Expand Down
19 changes: 13 additions & 6 deletions event-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ class EventHandler {
if (isEndState(afterState)) {
const channel = this.eventChannelManager.getChannelByEventFilterId(eventFilterId);
const clientFilterId = this.getClientFilterIdFromGlobalFilterId(eventFilterId);
if (!clientFilterId) {
continue;
}
this.eventChannelManager.deregisterFilterAndEmitEvent(
channel, clientFilterId, FilterDeletionReasons.END_STATE_REACHED
);
Expand All @@ -213,17 +216,21 @@ class EventHandler {
this.eventFilterIdToTimeoutCallback.set(eventFilterId, setTimeout(() => {
const channel = this.eventChannelManager.getChannelByEventFilterId(eventFilterId);
const clientFilterId = this.getClientFilterIdFromGlobalFilterId(eventFilterId);
if (!clientFilterId) {
return;
}
this.eventChannelManager.deregisterFilterAndEmitEvent(
channel, clientFilterId, FilterDeletionReasons.FILTER_TIMEOUT
);
}, NodeConfigs.EVENT_HANDLER_FILTER_DELETION_TIMEOUT_MS));
}

getClientFilterIdFromGlobalFilterId(globalFilterId) {
const LOG_HEADER = 'getClientFilterIdFromGlobalFilterId';
const clientFilterId = globalFilterId.split(':')[1];
if (!clientFilterId) {
throw new EventHandlerError(EventHandlerErrorCode.PARSING_GLOBAL_FILTER_ID_FAILURE,
`Can't get client filter ID from global filter ID (globalFilterId: ${globalFilterId})`);
logger.error(`[${LOG_HEADER}] Can't get client filter ID from global filter ID (globalFilterId: ${globalFilterId})`);
return null;
}
return clientFilterId;
}
Expand Down Expand Up @@ -304,13 +311,13 @@ class EventHandler {
const eventFilterId = this.getGlobalFilterId(channelId, clientFilterId);
const eventFilter = this.eventFilters[eventFilterId];
if (!eventFilter) {
throw new EventHandlerError(EventHandlerErrorCode.NO_MATCHED_FILTERS,
`Can't find filter by filter id`, eventFilterId);
logger.error(`[${LOG_HEADER}] Can't find filter by filter id (eventFilterId: ${eventFilterId})`);
return null;
}
delete this.eventFilters[eventFilterId];
if (!this.eventTypeToEventFilterIds[eventFilter.type].delete(eventFilterId)) {
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_IN_TYPE_TO_FILTER_IDS,
`Can't delete filter Id from eventTypeToEventFilterIds (${eventFilterId})`);
logger.error(`[${LOG_HEADER}] Can't delete filter Id from eventTypeToEventFilterIds (eventFilterId: ${eventFilterId})`);
return null;
}
if (eventFilter.type === BlockchainEventTypes.VALUE_CHANGED) {
this.stateEventTreeManager.deregisterEventFilterId(eventFilterId);
Expand Down
16 changes: 8 additions & 8 deletions event-handler/state-event-tree-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,24 @@ class StateEventTreeManager {
}

deleteFilterIdFromEventNode(eventNode, filterId) {
const LOG_HEADER = 'deleteFilterIdFromEventNode';
if (!eventNode || !eventNode.filterIdSet) {
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_SET,
`Can't find filterIdSet (eventNode: ${JSON.stringify(eventNode)})`);
logger.error(`[${LOG_HEADER}] Can't find filterIdSet (eventNode: ${JSON.stringify(eventNode)})`);
return;
}
if (!eventNode.filterIdSet.delete(filterId)) {
throw new EventHandlerError(EventHandlerErrorCode.MISSING_FILTER_ID_IN_FILTER_ID_SET,
`Can't delete filter id (${filterId}) from filterIdSet ` +
logger.error(`[${LOG_HEADER}] Can't delete filter id (${filterId}) from filterIdSet ` +
`(${JSON.stringify(eventNode.filterIdSet.values())})`);
return;
}
}

deregisterEventFilterId(filterId) {
const LOG_HEADER = 'deregisterEventFilterId';
const parsedPath = this.filterIdToParsedPath[filterId];
if (!parsedPath) {
throw new EventHandlerError(
EventHandlerErrorCode.MISSING_FILTER_ID_IN_FILTER_ID_TO_PARSED_PATH,
`Can't find parsedPath from filterIdToParsedPath (${filterId})`
);
logger.error(`[${LOG_HEADER}] Can't find parsedPath from filterIdToParsedPath (filterId: ${filterId})`);
return;
}
delete this.filterIdToParsedPath[filterId];

Expand Down
20 changes: 0 additions & 20 deletions test/integration/event_handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,26 +254,6 @@ describe('Event Handler Test', function() {
deregisterFilter(wsClient, filterId);
});

it('Deregister filter already deregisterd filter', function(done) {
// Deregister filter
wsClient.on('message', (message) => {
try {
const parsedMessage = JSON.parse(message);
const messageType = parsedMessage.type;
const errorCode = _.get(parsedMessage, 'data.code');
const errorMessage = _.get(parsedMessage, 'data.message');
if (messageType === BlockchainEventMessageTypes.EMIT_ERROR) {
expect(errorCode).to.equal(EventHandlerErrorCode.FAILED_TO_DEREGISTER_FILTER);
expect(errorMessage).to.equal(`Failed to deregister filter with filter ID: ${filterId} due to error: Can't find filter by filter id`)
done();
}
} catch (err) {
done(err);
}
});
deregisterFilter(wsClient, filterId);
});

it('Register too many filters', function(done) {
let exceededCnt = 0;
wsClient.on('message', (message) => {
Expand Down
Loading