@@ -15,7 +15,7 @@ MPI.size()
15
15
// returns the size of the cluster (the number of nodes)
16
16
```
17
17
``` js
18
- MPI .recv (msgType,callback)
18
+ MPI .recv (msgType, callback)
19
19
function callback (data ){
20
20
}
21
21
// starts to listen on all messages with msgType type and invokes callback on each message with the content as first parameter
@@ -64,80 +64,80 @@ Available for node.js version 4.x or upper.
64
64
65
65
Basic example of 3 phase commit algorithm using mpi-node library:
66
66
``` js
67
- var MPI = require (' mpi-node' );
67
+ const MPI = require (' mpi-node' );
68
68
MPI .init (main);
69
- function main () {
70
- var state = ' Begin '
71
- var tid = MPI . rank ()
72
- var timeoutTime = 5000 ;
73
- var timeout ;
74
- var numOfAnswers = 0 ;
75
- if (tid == 0 ){
69
+ function main () {
70
+ const tid = MPI . rank ();
71
+ const timeoutTime = 5000 ;
72
+ let state = ' Begin ' ;
73
+ let numOfAnswers = 0 ;
74
+ let timeout ;
75
+ if (tid === 0 ) {
76
76
sendCommitRequest ();
77
- setInterval (sendCommitRequest,1000 );
78
- function sendCommitRequest () {
79
- MPI .broadcast ({type: ' CanCommit' , content: ' transaction' })
80
- state = ' Waiting'
81
- timeout = setTimeout (function () {
82
- MPI .broadcast ({type: ' Abort' , content: ' transaction' })
83
- state = ' Aborted'
84
- },timeoutTime)
77
+ setInterval (sendCommitRequest, 1000 );
78
+ function sendCommitRequest () {
79
+ MPI .broadcast ({type: ' CanCommit' , content: ' transaction' });
80
+ state = ' Waiting' ;
81
+ timeout = setTimeout (() => {
82
+ MPI .broadcast ({type: ' Abort' , content: ' transaction' });
83
+ state = ' Aborted' ;
84
+ }, timeoutTime);
85
85
}
86
- MPI .recv (' Yes' ,function (message ){
86
+ MPI .recv (' Yes' , (message ) => {
87
87
numOfAnswers++ ;
88
- if (numOfAnswers== MPI .size ()- 1 ){
89
- numOfAnswers= 0 ;
88
+ if (numOfAnswers === MPI .size () - 1 ){
89
+ numOfAnswers = 0 ;
90
90
clearTimeout (timeout);
91
- MPI .broadcast ({type: ' preCommit' , content: ' transaction' })
92
- state = ' Prepared'
93
- timeout = setTimeout (function () {
94
- MPI .broadcast ({type: ' Abort' , content: ' transaction' })
95
- state = ' Aborted'
96
- },timeoutTime)
91
+ MPI .broadcast ({type: ' preCommit' , content: ' transaction' });
92
+ state = ' Prepared' ;
93
+ timeout = setTimeout (() => {
94
+ MPI .broadcast ({type: ' Abort' , content: ' transaction' });
95
+ state = ' Aborted' ;
96
+ }, timeoutTime);
97
97
}
98
98
})
99
- MPI .recv (' No' ,function (message ){
100
- MPI .broadcast ({type: ' Abort' , content: ' transaction' })
101
- state = ' Aborted'
99
+ MPI .recv (' No' , (message ) => {
100
+ MPI .broadcast ({type: ' Abort' , content: ' transaction' });
101
+ state = ' Aborted' ;
102
102
})
103
- MPI .recv (' ACK' ,function (message ){
103
+ MPI .recv (' ACK' , (message ) => {
104
104
numOfAnswers++ ;
105
- if (numOfAnswers== MPI .size ()- 1 ){
106
- numOfAnswers= 0 ;
105
+ if (numOfAnswers === MPI .size () - 1 ){
106
+ numOfAnswers = 0 ;
107
107
clearTimeout (timeout);
108
- MPI .broadcast ({type: ' doCommit' , content: ' transaction' })
109
- state = ' Commited'
108
+ MPI .broadcast ({type: ' doCommit' , content: ' transaction' });
109
+ state = ' Commited' ;
110
110
}
111
111
});
112
112
}
113
- else {
114
- MPI .recv (' CanCommit' ,function (message ){
115
- timeout = setTimeout (abortHandler,timeoutTime);
116
- MPI .send (0 ,{type: ' Yes' , content: ' transaction' })
113
+ else {
114
+ MPI .recv (' CanCommit' , (message ) => {
115
+ timeout = setTimeout (abortHandler, timeoutTime);
116
+ MPI .send (0 , {type: ' Yes' , content: ' transaction' });
117
117
state = ' Waiting' ;
118
118
})
119
- MPI .recv (' abort' ,abortHandler);
120
- MPI .recv (' preCommit' ,function (message ){
119
+ MPI .recv (' abort' , abortHandler);
120
+ MPI .recv (' preCommit' , (message ) => {
121
121
clearTimeout (timeout);
122
- timeout = setTimeout (commitHandler,timeoutTime);
123
- MPI .send (0 ,{type: ' ACK' , content: ' transaction' })
124
- state = ' Prepared'
122
+ timeout = setTimeout (commitHandler, timeoutTime);
123
+ MPI .send (0 , {type: ' ACK' , content: ' transaction' });
124
+ state = ' Prepared' ;
125
125
})
126
- MPI .recv (' doCommit' ,commitHandler)
126
+ MPI .recv (' doCommit' , commitHandler);
127
127
}
128
- function commitHandler () {
129
- if (timeout){
128
+ function commitHandler () {
129
+ if (timeout) {
130
130
clearTimeout (timeout);
131
131
}
132
- state= ' Commited' ;
132
+ state = ' Commited' ;
133
133
}
134
134
function abortHandler (message ){
135
- if (timeout){
135
+ if (timeout) {
136
136
clearTimeout (timeout);
137
- timeout= null ;
137
+ timeout = null ;
138
138
}
139
- state= ' aborted ' ;
139
+ state = ' Aborted ' ;
140
140
}
141
141
` ` `
142
142
143
- [mpi-node-cli]: <https://www.npmjs.com/package/mpi-node-cli>
143
+ [mpi-node-cli]: <https://www.npmjs.com/package/mpi-node-cli>
0 commit comments