node-test-bed-adapter
Version:
An adapter to connect a node.js application to the Test-bed's Common Information Space or Common Simulation Space.
188 lines (187 loc) • 7.37 kB
text/typescript
import { Kafka, Message, RecordMetadata, ITopicConfig, KafkaConfig, ITopicMetadata } from 'kafkajs';
import { EventEmitter } from 'events';
import { ITestBedOptions, AdapterProducerRecord, AdapterMessage } from './models/index.mjs';
import { Type } from 'avsc';
import { IAdminHeartbeat, TimeState, ITimeManagement } from 'test-bed-schemas';
export interface OffsetOutOfRange {
message: string;
partition: number;
stack: string;
topic: string;
}
export interface TestBedAdapter {
on(event: 'ready', listener: () => void): this;
on(event: 'connect', listener: () => void): this;
on(event: 'consumer.heartbeat', listener: () => void): this;
on(event: 'consumer.commit_offsets', listener: () => void): this;
on(event: 'consumer.group_join', listener: () => void): this;
on(event: 'consumer.fetch_start', listener: () => void): this;
on(event: 'consumer.fetch', listener: () => void): this;
on(event: 'consumer.start_batch_process', listener: () => void): this;
on(event: 'consumer.end_batch_process', listener: () => void): this;
on(event: 'consumer.connect', listener: () => void): this;
on(event: 'consumer.disconnect', listener: () => void): this;
on(event: 'consumer.stop', listener: () => void): this;
on(event: 'consumer.crash', listener: () => void): this;
on(event: 'consumer.rebalancing', listener: () => void): this;
on(event: 'consumer.received_unsubscribed_topics', listener: () => void): this;
on(event: 'consumer.network.request', listener: () => void): this;
on(event: 'consumer.network.request_timeout', listener: () => void): this;
on(event: 'consumer.network.request_queue_size', listener: () => void): this;
on(event: 'reconnect', listener: () => void): this;
on(event: 'error', listener: (error: string) => void): this;
on(event: 'offsetOutOfRange', listener: (error: OffsetOutOfRange) => void): this;
on(event: 'raw', listener: (message: Message) => void): this;
on(event: 'message', listener: (message: AdapterMessage) => void): this;
on(event: 'time', listener: (message: ITimeManagement) => void): this;
on(event: 'heartbeat', listener: (message: IAdminHeartbeat) => void): this;
}
export declare class TestBedAdapter extends EventEmitter {
static HeartbeatInterval: number;
isConnected: boolean;
private groupId;
private schemaPublisher;
private schemaRegistry;
private largeFileUploadService;
private log;
private client?;
private producer?;
private consumer?;
private config;
/** Map of all initialized topics, i.e. with validators/encoders/decoders */
private consumerTopics;
private producerTopics;
/** Location of the configuration file */
private configFile;
private timeService;
private origin?;
constructor(config?: ITestBedOptions | string);
connect(): Promise<boolean>;
disconnect(): void;
/**
* Get the underlying KafkaJS client, if defined.
* Should be available after the 'ready' event is emitted.
*/
getClient(): Kafka | undefined;
/**
* A dictionary containing a clone of all the key schemas with key the bare topic name and
* value the instance of the AVRO schema and schema ID.
*/
get keySchemas(): {
[topic: string]: {
type: Type;
srId: number;
};
};
/**
* A dictionary containing a clone of all the value schemas with key the bare topic name and
* value the instance of the AVRO schema and schema ID.
*/
get valueSchemas(): {
[topic: string]: {
type: Type;
srId: number;
};
};
/** Pause all topics when no topics are specified, or only the ones that are specified */
pause(topics?: {
topic: string;
partitions?: number[] | undefined;
}[]): void;
/** Resume all topics when no topics are specified, or only the ones that are specified */
resume(topics?: {
topic: string;
partitions?: number[] | undefined;
}[]): void;
/** Deprecated: use disconnect */
close(): void;
private keyFactory;
send(payload: AdapterProducerRecord, cb: (error?: any, data?: RecordMetadata[]) => any): Promise<any>;
/**
* Returns (a clone of) the configuration options.
*/
get configuration(): ITestBedOptions & KafkaConfig;
/**
* Create topics by requesting their metadata.
* It only works when `auto.create.topics.enable = true`.
*/
createTopics(topics: ITopicConfig[]): Promise<boolean>;
/**
* Add topics (encoding utf8)
*
* @param topics Array of topics to add
*/
addConsumerTopics(topics?: string | string[]): Promise<void>;
addProducerTopics(topics?: string | string[]): Promise<void>;
/**
* Load the metadata for all topics (in case of an empty array), or specific ones.
*
* @param topics If topics is an empty array, retreive the metadata of all topics
* @param cb callback function to return the metadata results
*/
loadMetadataForTopics(topics: string[], cb: (error?: any, results?: ITopicMetadata[]) => void): Promise<void>;
/**
* Get the simulation time as UTC Date.
*/
get simulationTime(): Date;
/**
* Get the simulation state.
*/
get timeState(): TimeState;
/**
* Positive number, indicating how fast the simulation / trial time moves with respect
* to the actual time. A value of 0 means a pause, 1 is as fast as real-time.
*/
get simulationSpeed(): number;
/**
* Get elapsed time in msec.
*/
get timeElapsed(): number;
/**
* Upload a file to the large file service, if enabled.
* NOTE: the configuration needs to specify the URL of the large file service, e.g. http://localhost:9090
*/
uploadFile(file: string, isPrivate: boolean, cb?: (err?: Error, uploadUrl?: string) => void): void;
/** List of the uploaded schemas, if any */
get uploadedSchemas(): string[];
/** After the Kafka client is connected, initialize the other services too, starting with the schema registry. */
private initialize;
private initProducer;
private initLogger;
/** If required, add the Kafka logger too (after the producer has been initialised). */
private addKafkaLogger;
private initConsumer;
private handleMessage;
private registerTopic;
private unregisterTopic;
/**
* Add the topics to the configuration and initialize the decoders.
* @param topics topics to add
*/
private initializeConsumerTopics;
/**
* Add the topics to the configuration and initialize the encoders/validators.
* @param topics topics to add
*/
private initializeProducerTopics;
/**
* Start transmitting a heartbeat message.
*/
private startHeartbeat;
/**
* Set the default options of the configuration.
* @param options current configuration
*/
private setDefaultOptions;
/**
* Validate that all required options are set, or throw an error if not.
* @param options current configuration
*/
private validateOptions;
/**
* Load the configuration options from file.
* @param configFile configuration file path
*/
private loadOptionsFromFile;
private emitErrorMsg;
}