@@ -75,9 +75,11 @@ actions.defineHttp({
75
75
return ;
76
76
}
77
77
78
- if ( serverId . substr ( 0 , 4 ) !== 'CRDN' && serverId . substr ( 0 , 4 ) !== 'PRMR' ) {
79
- actions . resultError ( req , res , actions . HTTP_BAD ,
80
- 'couldn\'t determine role for server id ' + serverId ) ;
78
+ let isCoordinator = serverId . substr ( 0 , 4 ) === 'CRDN' ;
79
+ let isDBServer = serverId . substr ( 0 , 4 ) === 'PRMR' ;
80
+ if ( ! isCoordinator && ! isDBServer ) {
81
+ actions . resultError (
82
+ req , res , actions . HTTP_BAD , 'couldn\'t determine role for server id ' + serverId ) ;
81
83
return ;
82
84
}
83
85
@@ -87,37 +89,41 @@ actions.defineHttp({
87
89
while ( ++ count <= 60 ) {
88
90
// need to make sure it is not responsible for anything
89
91
used = [ ] ;
90
- let preconditions = reducePlanServers ( function ( data , agencyKey , servers ) {
91
- data [ agencyKey ] = { 'old' : servers } ;
92
- if ( servers . indexOf ( serverId ) !== - 1 ) {
93
- used . push ( agencyKey ) ;
94
- }
95
- return data ;
96
- } , { } ) ;
97
- preconditions = reduceCurrentServers ( function ( data , agencyKey , servers ) {
98
- data [ agencyKey ] = { 'old' : servers } ;
99
- if ( servers . indexOf ( serverId ) !== - 1 ) {
100
- used . push ( agencyKey ) ;
101
- }
102
- return data ;
103
- } , preconditions ) ;
92
+ let curVersion = amongCurrentShardsOrVersion ( serverId ) ;
93
+ if ( curVersion === 0 ) {
94
+ wait ( 1.0 ) ;
95
+ continue ;
96
+ }
97
+ let planVersion = amongPlanShardsOrVersion ( serverId ) ;
98
+ if ( planVersion === 0 ) {
99
+ wait ( 1.0 ) ;
100
+ continue ;
101
+ }
104
102
103
+ var preconditions = { } ;
104
+ preconditions [ '/arango/Plan/Version' ] = { 'old' : planVersion } ;
105
+ preconditions [ '/arango/Current/Version' ] = { 'old' : curVersion } ;
105
106
preconditions [ '/arango/Supervision/Health/' + serverId + '/Status' ] = { 'old' : 'FAILED' } ;
106
- preconditions [ "/arango/Supervision/DBServers/" + serverId ]
107
- = { "oldEmpty" : true } ;
107
+ if ( isDBServer ) {
108
+ preconditions [ "/arango/Supervision/DBServers/" + serverId ] = { "oldEmpty" : true } ;
109
+ }
108
110
109
- if ( ! checkServerLocked ( serverId ) && used . length === 0 ) {
111
+ if ( ! checkServerLocked ( serverId ) ) {
110
112
let operations = { } ;
111
- operations [ '/arango/Plan/Coordinators/' + serverId ] = { 'op' : 'delete' } ;
112
- operations [ '/arango/Plan/DBServers/' + serverId ] = { 'op' : 'delete' } ;
113
+ if ( isCoordinator ) {
114
+ operations [ '/arango/Plan/Coordinators/' + serverId ] = { 'op' : 'delete' } ;
115
+ operations [ '/arango/Current/Coordinators/' + serverId ] = { 'op' : 'delete' } ;
116
+ } else {
117
+ operations [ '/arango/Plan/DBServers/' + serverId ] = { 'op' : 'delete' } ;
118
+ operations [ '/arango/Current/DBServers/' + serverId ] = { 'op' : 'delete' } ;
119
+ }
113
120
operations [ '/arango/Current/ServersRegistered/' + serverId ] = { 'op' : 'delete' } ;
114
- operations [ '/arango/Current/DBServers/' + serverId ] = { 'op' : 'delete' } ;
115
- operations [ '/arango/Current/Coordinators/' + serverId ] = { 'op' : 'delete' } ;
116
121
operations [ '/arango/Supervision/Health/' + serverId ] = { 'op' : 'delete' } ;
117
122
operations [ '/arango/Target/MapUniqueToShortID/' + serverId ] = { 'op' : 'delete' } ;
118
123
operations [ '/arango/Current/ServersKnown/' + serverId ] = { 'op' : 'delete' } ;
119
124
operations [ '/arango/Target/RemovedServers/' + serverId ] = { 'op' : 'set' , 'new' : ( new Date ( ) ) . toISOString ( ) } ;
120
125
126
+ console . info ( [ [ operations , preconditions ] ] ) ;
121
127
try {
122
128
global . ArangoAgency . write ( [ [ operations , preconditions ] ] ) ;
123
129
actions . resultOk ( req , res , actions . HTTP_OK , true ) ;
@@ -141,8 +147,7 @@ actions.defineHttp({
141
147
wait ( 1.0 ) ;
142
148
} // while count
143
149
actions . resultError ( req , res , actions . HTTP_PRECONDITION_FAILED ,
144
- 'the server not failed, locked or is still in use at the following '
145
- + 'locations: ' + JSON . stringify ( used ) ) ;
150
+ 'the server is either not failed or is locked or is still in use.' ) ;
146
151
}
147
152
} ) ;
148
153
@@ -638,6 +643,36 @@ function reducePlanServers (reducer, data) {
638
643
} , data ) ;
639
644
}
640
645
646
+ function amongCurrentShardsOrVersion ( myId ) {
647
+ var current = ArangoAgency . get ( 'Current' ) . arango . Current ;
648
+ for ( const [ name , database ] of Object . entries ( current . Collections ) ) {
649
+ for ( const [ name , collection ] of Object . entries ( database ) ) {
650
+ for ( const [ name , shard ] of Object . entries ( collection ) ) {
651
+ if ( shard . servers . indexOf ( myId ) > - 1 ) {
652
+ // i hit a shard with my id listed
653
+ return 0 ;
654
+ }
655
+ }
656
+ }
657
+ }
658
+ return current . Version ;
659
+ }
660
+
661
+ function amongPlanShardsOrVersion ( myId ) {
662
+ var plan = ArangoAgency . get ( 'Plan' ) . arango . Plan ;
663
+ for ( const [ name , database ] of Object . entries ( plan . Collections ) ) {
664
+ for ( const [ name , collection ] of Object . entries ( database ) ) {
665
+ for ( const [ name , shard ] of Object . entries ( collection . shards ) ) {
666
+ if ( shard . indexOf ( myId ) > - 1 ) {
667
+ // i hit a shard with my id listed
668
+ return 0 ;
669
+ }
670
+ }
671
+ }
672
+ }
673
+ return plan . Version ;
674
+ }
675
+
641
676
function reduceCurrentServers ( reducer , data ) {
642
677
var databases = ArangoAgency . get ( 'Current/Collections' ) ;
643
678
databases = databases . arango . Current . Collections ;
0 commit comments