View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.omid.tso;
19  
20  import com.google.common.util.concurrent.AbstractIdleService;
21  import com.google.inject.Binder;
22  import com.google.inject.Guice;
23  import com.google.inject.Inject;
24  import com.google.inject.Injector;
25  import com.google.inject.Module;
26  import com.google.inject.Singleton;
27  import org.apache.omid.metrics.MetricsRegistry;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  import java.io.IOException;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  @Singleton
36  public class TSOServer extends AbstractIdleService {
37  
38      private static final Logger LOG = LoggerFactory.getLogger(TSOServer.class);
39  
40      public static final String DASH_SEPARATOR_80_CHARS =
41              "--------------------------------------------------------------------------------";
42  
43      public static final String TSO_HOST_AND_PORT_KEY = "tso.hostandport";
44  
45      @Inject
46      private TSOStateManager tsoStateManager;
47      @Inject
48      private RequestProcessor requestProcessor;
49      @Inject
50      private PersistenceProcessor persistenceProcessor;
51      @Inject
52      private RetryProcessor retryProcessor;
53      @Inject
54      private ReplyProcessor replyProcessor;
55  
56      // ----------------------------------------------------------------------------------------------------------------
57      // High availability related variables
58      // ----------------------------------------------------------------------------------------------------------------
59  
60      @Inject
61      private LeaseManagement leaseManagement;
62  
63      // ----------------------------------------------------------------------------------------------------------------
64  
65      static TSOServer getInitializedTsoServer(TSOServerConfig config) throws IOException {
66          LOG.info("Configuring TSO Server...");
67          Injector injector = Guice.createInjector(buildModuleList(config));
68          LOG.info("TSO Server configured. Creating instance...");
69          return injector.getInstance(TSOServer.class);
70      }
71  
72      private static List<Module> buildModuleList(final TSOServerConfig config) throws IOException {
73  
74          List<Module> guiceModules = new ArrayList<>();
75          guiceModules.add(config.getTimestampStoreModule());
76          guiceModules.add(config.getCommitTableStoreModule());
77          guiceModules.add(config.getLeaseModule());
78          guiceModules.add(new TSOModule(config));
79  
80          guiceModules.add(new Module() {
81              @Override
82              public void configure(Binder binder) {
83                  LOG.info("\t* Metrics provider module set to {}", config.getMetrics().getClass());
84                  binder.bind(MetricsRegistry.class).toInstance(config.getMetrics());
85              }
86          });
87          return guiceModules;
88      }
89  
90      // ----------------------------------------------------------------------------------------------------------------
91      // AbstractIdleService implementation
92      // ----------------------------------------------------------------------------------------------------------------
93  
94      @Override
95      protected void startUp() throws Exception {
96          LOG.info("{}", DASH_SEPARATOR_80_CHARS);
97          LOG.info("Starting TSO Server");
98          LOG.info("{}", DASH_SEPARATOR_80_CHARS);
99          tsoStateManager.register(requestProcessor);
100         leaseManagement.startService();
101         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
102         if (leaseManagement instanceof VoidLeaseManager) {
103             LOG.info("TSO Server running and accepting connections");
104         } else if (leaseManagement instanceof LeaseManager) {
105             LOG.info("TSO Server running on HA mode. Waiting to be signaled as the Master replica...");
106         } else {
107             throw new RuntimeException("Wrong TSO mode");
108         }
109         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
110     }
111 
112     @Override
113     protected void shutDown() throws Exception {
114         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
115         LOG.info("Shutting Down TSO Server");
116         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
117         leaseManagement.stopService();
118         tsoStateManager.unregister(requestProcessor);
119         requestProcessor.close();
120         persistenceProcessor.close();
121         retryProcessor.close();
122         replyProcessor.close();
123         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
124         LOG.info("TSO Server stopped");
125         LOG.info("{}", DASH_SEPARATOR_80_CHARS);
126     }
127 
128     // ----------------------------------------------------------------------------------------------------------------
129 
130     private void attachShutDownHook() {
131         Runtime.getRuntime().addShutdownHook(new Thread() {
132             @Override
133             public void run() {
134                 stopAndWait();
135             }
136         });
137         LOG.info("Shutdown Hook Attached");
138     }
139 
140     /**
141      * This is where all starts on the server side
142      */
143     public static void main(String[] args) {
144 
145         TSOServerConfig config = new TSOServerConfig();
146 
147         try {
148             TSOServer tsoServer = getInitializedTsoServer(config);
149             tsoServer.attachShutDownHook();
150             tsoServer.startAndWait();
151         } catch (Exception e) {
152             System.out.println(e.getMessage());
153             System.exit(-1);
154         }
155 
156     }
157 
158 }