3
3
* <p>
4
4
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5
5
* the License. You may obtain a copy of the License at
6
- *
6
+ *
7
7
* http://www.apache.org/licenses/LICENSE-2.0
8
- *
8
+ *
9
9
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10
10
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11
11
* specific language governing permissions and limitations under the License.
17
17
import java .util .concurrent .atomic .AtomicBoolean ;
18
18
19
19
import com .vip .saturn .job .internal .storage .JobNodeStorage ;
20
+ import com .vip .saturn .job .utils .LogUtils ;
20
21
import org .slf4j .Logger ;
21
22
import org .slf4j .LoggerFactory ;
22
23
26
27
27
28
/**
28
29
* 选举主节点的服务.
29
- *
30
- *
30
+ *
31
+ *
31
32
*/
32
33
public class LeaderElectionService extends AbstractSaturnService {
33
34
static Logger log = LoggerFactory .getLogger (LeaderElectionService .class );
@@ -44,13 +45,14 @@ public void shutdown() {
44
45
if (isShutdown .compareAndSet (false , true )) {
45
46
try { // Release my leader position
46
47
JobNodeStorage jobNodeStorage = getJobNodeStorage ();
47
- if (jobNodeStorage .isConnected ()
48
- && executorName .equals (jobNodeStorage .getJobNodeDataDirectly (ElectionNode .LEADER_HOST ))) {
48
+ if (jobNodeStorage .isConnected () && executorName
49
+ .equals (jobNodeStorage .getJobNodeDataDirectly (ElectionNode .LEADER_HOST ))) {
49
50
jobNodeStorage .removeJobNodeIfExisted (ElectionNode .LEADER_HOST );
50
- log .info ("[{}] msg={} that was {}'s leader, released itself" , jobName , executorName , jobName );
51
+ LogUtils .info (log , jobName , "[{}] msg={} that was {}'s leader, released itself" , jobName ,
52
+ executorName , jobName );
51
53
}
52
54
} catch (Throwable t ) {
53
- log .error (t .getMessage (), t );
55
+ LogUtils .error (log , jobName , t .getMessage (), t );
54
56
}
55
57
}
56
58
}
@@ -70,15 +72,15 @@ public void leaderElection() {
70
72
*/
71
73
public Boolean isLeader () {
72
74
while (!isShutdown .get () && !hasLeader ()) {
73
- log .info ("[{}] msg=No leader, try to election" , jobName );
75
+ LogUtils .info (log , jobName , "[{}] msg=No leader, try to election" , jobName );
74
76
leaderElection ();
75
77
}
76
78
return executorName .equals (getJobNodeStorage ().getJobNodeDataDirectly (ElectionNode .LEADER_HOST ));
77
79
}
78
80
79
81
/**
80
82
* 判断是否已经有主节点
81
- *
83
+ *
82
84
* @return 是否已经有主节点
83
85
*/
84
86
public boolean hasLeader () {
@@ -95,7 +97,8 @@ public void execute() {
95
97
}
96
98
if (!getJobNodeStorage ().isJobNodeExisted (ElectionNode .LEADER_HOST )) {
97
99
getJobNodeStorage ().fillEphemeralJobNode (ElectionNode .LEADER_HOST , executorName );
98
- log .info ("[{}] msg=executor {} become job {}'s leader" , jobName , executorName , jobName );
100
+ LogUtils .info (log , jobName , "[{}] msg=executor {} become job {}'s leader" , jobName , executorName ,
101
+ jobName );
99
102
}
100
103
}
101
104
}
0 commit comments