1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002, 2014 Oracle and/or its affiliates.  All rights reserved.
5  *
6  */
7 
8 package com.sleepycat.je.rep.impl;
9 
10 import java.io.IOException;
11 import java.util.logging.Logger;
12 
13 import com.sleepycat.je.rep.impl.NodeStateProtocol.NodeStateRequest;
14 import com.sleepycat.je.rep.impl.TextProtocol.RequestMessage;
15 import com.sleepycat.je.rep.impl.TextProtocol.ResponseMessage;
16 import com.sleepycat.je.rep.impl.node.RepNode;
17 import com.sleepycat.je.rep.net.DataChannel;
18 import com.sleepycat.je.rep.utilint.ServiceDispatcher;
19 import com.sleepycat.je.rep.utilint.ServiceDispatcher.ExecutingService;
20 import com.sleepycat.je.rep.utilint.ServiceDispatcher.ExecutingRunnable;
21 import com.sleepycat.je.utilint.LoggerUtils;
22 
23 /**
24  * The service registered by a RepNode to answer the state request from
25  * another node. It can also be extended to be used by "Ping" command.
26  */
27 public class NodeStateService extends ExecutingService {
28 
29     private final RepNode repNode;
30     private final NodeStateProtocol protocol;
31     private final Logger logger;
32 
33     /* Identifies the Node State querying Service. */
34     public static final String SERVICE_NAME = "NodeState";
35 
NodeStateService(ServiceDispatcher dispatcher, RepNode repNode)36     public NodeStateService(ServiceDispatcher dispatcher, RepNode repNode) {
37         super(SERVICE_NAME, dispatcher);
38         this.repNode = repNode;
39 
40         String groupName =
41             repNode.getRepImpl().cloneRepConfig().getGroupName();
42         protocol = new NodeStateProtocol
43             (groupName, repNode.getNameIdPair(), repNode.getRepImpl(),
44              dispatcher.getChannelFactory());
45         logger = LoggerUtils.getLogger(getClass());
46     }
47 
48     /**
49      * Process a node state querying request.
50      */
51     @SuppressWarnings("unused")
process(NodeStateRequest stateRequest)52     public ResponseMessage process(NodeStateRequest stateRequest) {
53         long joinTime = repNode.getMonitorEventManager().getJoinTime();
54         return protocol.new NodeStateResponse(repNode.getNodeName(),
55                                               repNode.getMasterName(),
56                                               joinTime,
57                                               repNode.getRepImpl().getState());
58     }
59 
60     @Override
getRunnable(DataChannel dataChannel)61     public Runnable getRunnable(DataChannel dataChannel) {
62         return new NodeStateServiceRunnable(dataChannel, protocol);
63     }
64 
65     class NodeStateServiceRunnable extends ExecutingRunnable {
NodeStateServiceRunnable(DataChannel dataChannel, NodeStateProtocol protocol)66         NodeStateServiceRunnable(DataChannel dataChannel,
67                                  NodeStateProtocol protocol) {
68             super(dataChannel, protocol, true);
69         }
70 
71         @Override
getResponse(RequestMessage request)72         protected ResponseMessage getResponse(RequestMessage request)
73             throws IOException {
74 
75             return protocol.process(NodeStateService.this, request);
76         }
77 
78         @Override
logMessage(String message)79         protected void logMessage(String message) {
80             LoggerUtils.warning(logger, repNode.getRepImpl(), message);
81         }
82     }
83 }
84