-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmulti-node.ts
More file actions
112 lines (99 loc) · 3.09 KB
/
Copy pathmulti-node.ts
File metadata and controls
112 lines (99 loc) · 3.09 KB
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
/**
* 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 with string array format (RECOMMENDED)
console.log('Method 1: Using nodeUrls in string array format (host:port)');
const pool1 = new SessionPool({
nodeUrls: [
'node1.example.com:6667',
'node2.example.com:6668',
'node3.example.com:6669',
],
username: 'root',
password: 'root',
maxPoolSize: 15, // 5 connections per node
minPoolSize: 3, // 1 connection per node initially
});
// Method 2: Using Builder pattern with string array format (RECOMMENDED)
console.log('Method 2: Using Builder pattern with string array');
const pool2 = new SessionPool(
new PoolConfigBuilder()
.nodeUrls([
'node1.example.com:6667',
'node2.example.com:6668',
'node3.example.com:6669',
])
.username('root')
.password('root')
.maxPoolSize(15)
.minPoolSize(3)
.build()
);
// Method 3: Using nodeUrls with object format (also supported)
console.log('Method 3: Using nodeUrls in object format');
const pool3 = 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,
minPoolSize: 3,
});
// Method 4: Backward compatible - same port for all hosts
console.log('Method 4: Backward compatible (same port for all hosts)');
const pool4 = 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 dataSet = await pool.executeQueryStatement('SHOW DATABASES');
let count = 0;
while (await dataSet.hasNext()) {
dataSet.next();
count++;
}
await dataSet.close();
console.log(`Query ${i + 1} executed (result count: ${count})`);
}
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();
await pool2.close();
await pool3.close();
await pool4.close();
console.log('Pool closed');
}
}
main().catch(console.error);