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 TSO_HOST_AND_PORT_KEY = "tso.hostandport";
41  
42      @Inject
43      private TSOStateManager tsoStateManager;
44      @Inject
45      private RequestProcessor requestProcessor;
46  
47      // ----------------------------------------------------------------------------------------------------------------
48      // High availability related variables
49      // ----------------------------------------------------------------------------------------------------------------
50  
51      @Inject
52      private LeaseManagement leaseManagement;
53  
54      // ----------------------------------------------------------------------------------------------------------------
55  
56      static TSOServer getInitializedTsoServer(TSOServerConfig config) throws IOException {
57          LOG.info("Configuring TSO Server...");
58          Injector injector = Guice.createInjector(buildModuleList(config));
59          LOG.info("TSO Server configured. Creating instance...");
60          return injector.getInstance(TSOServer.class);
61      }
62  
63      private static List<Module> buildModuleList(final TSOServerConfig config) throws IOException {
64  
65          List<Module> guiceModules = new ArrayList<>();
66          guiceModules.add(config.getTimestampStoreModule());
67          guiceModules.add(config.getCommitTableStoreModule());
68          guiceModules.add(config.getLeaseModule());
69          guiceModules.add(new TSOModule(config));
70  
71          guiceModules.add(new Module() {
72              @Override
73              public void configure(Binder binder) {
74                  LOG.info("\t* Metrics provider module set to {}", config.getMetrics().getClass());
75                  binder.bind(MetricsRegistry.class).toInstance(config.getMetrics());
76              }
77          });
78          return guiceModules;
79      }
80  
81      // ----------------------------------------------------------------------------------------------------------------
82      // AbstractIdleService implementation
83      // ----------------------------------------------------------------------------------------------------------------
84  
85      @Override
86      protected void startUp() throws Exception {
87          tsoStateManager.register(requestProcessor);
88          leaseManagement.startService();
89          LOG.info("********** TSO Server running **********");
90      }
91  
92      @Override
93      protected void shutDown() throws Exception {
94          leaseManagement.stopService();
95          tsoStateManager.unregister(requestProcessor);
96          LOG.info("********** TSO Server stopped successfully **********");
97      }
98  
99      // ----------------------------------------------------------------------------------------------------------------
100 
101     private void attachShutDownHook() {
102         Runtime.getRuntime().addShutdownHook(new Thread() {
103             @Override
104             public void run() {
105                 stopAndWait();
106             }
107         });
108         LOG.info("Shutdown Hook Attached");
109     }
110 
111     /**
112      * This is where all starts on the server side
113      */
114     public static void main(String[] args) {
115 
116         TSOServerConfig config = new TSOServerConfig();
117 
118         try {
119             TSOServer tsoServer = getInitializedTsoServer(config);
120             tsoServer.attachShutDownHook();
121             tsoServer.startAndWait();
122         } catch (Exception e) {
123             System.out.println(e.getMessage());
124             System.exit(-1);
125         }
126 
127     }
128 
129 }