blob: c067babd0f41654ec6fc1e9f9d0ee6f0253a4ba6 [file]
/**
* Multi-Node Example
*
* This example demonstrates how to configure SessionPool to work with
* multiple IoTDB nodes for load balancing using the new nodeUrls API.
*/
import { SessionPool, PoolConfigBuilder } from '../src';
async function main() {
console.log('=== Multi-Node Example ===\n');
// Method 1: Using nodeUrls directly in constructor
console.log('Method 1: Using nodeUrls in config object');
const pool1 = new SessionPool({
nodeUrls: [
{ host: 'node1.example.com', port: 6667 },
{ host: 'node2.example.com', port: 6668 },
{ host: 'node3.example.com', port: 6669 },
],
username: 'root',
password: 'root',
maxPoolSize: 15, // 5 connections per node
minPoolSize: 3, // 1 connection per node initially
});
// Method 2: Using Builder pattern (recommended)
console.log('Method 2: Using Builder pattern');
const pool2 = new SessionPool(
new PoolConfigBuilder()
.nodeUrls([
{ host: 'node1.example.com', port: 6667 },
{ host: 'node2.example.com', port: 6668 },
{ host: 'node3.example.com', port: 6669 },
])
.username('root')
.password('root')
.maxPoolSize(15)
.minPoolSize(3)
.build()
);
// Method 3: Backward compatible - same port for all hosts
console.log('Method 3: Backward compatible (same port for all hosts)');
const pool3 = new SessionPool(
[
'node1.example.com',
'node2.example.com',
'node3.example.com',
],
6667,
{
username: 'root',
password: 'root',
maxPoolSize: 15,
minPoolSize: 3,
}
);
// For demo purposes, we'll use pool1
const pool = pool1;
try {
console.log('\nInitializing multi-node session pool...');
await pool.init();
console.log('Pool initialized with', pool.getPoolSize(), 'connections across 3 nodes');
// Execute operations
// Connections will be distributed across all nodes using round-robin
console.log('\nExecuting operations across nodes...');
for (let i = 0; i < 10; i++) {
const result = await pool.executeQueryStatement('SHOW DATABASES');
console.log(`Query ${i + 1} executed (result count: ${result.rows.length})`);
}
console.log('\nAll operations completed');
console.log('Final pool size:', pool.getPoolSize());
console.log('Available connections:', pool.getAvailableSize());
} catch (error) {
console.error('Error:', error);
} finally {
await pool.close();
console.log('Pool closed');
}
}
main().catch(console.error);