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  
19  package org.apache.hadoop.hbase.backup.master;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.concurrent.ThreadPoolExecutor;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.CoordinatedStateManagerFactory;
29  import org.apache.hadoop.hbase.ServerName;
30  import org.apache.hadoop.hbase.coordination.BaseCoordinatedStateManager;
31  import org.apache.hadoop.hbase.errorhandling.ForeignException;
32  import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher;
33  import org.apache.hadoop.hbase.master.MasterServices;
34  import org.apache.hadoop.hbase.master.MetricsMaster;
35  import org.apache.hadoop.hbase.procedure.MasterProcedureManager;
36  import org.apache.hadoop.hbase.procedure.Procedure;
37  import org.apache.hadoop.hbase.procedure.ProcedureCoordinator;
38  import org.apache.hadoop.hbase.procedure.ProcedureCoordinatorRpcs;
39  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.NameStringPair;
40  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ProcedureDescription;
41  import org.apache.zookeeper.KeeperException;
42  
43  public class LogRollMasterProcedureManager extends MasterProcedureManager {
44  
45    public static final String ROLLLOG_PROCEDURE_SIGNATURE = "rolllog-proc";
46    public static final String ROLLLOG_PROCEDURE_NAME = "rolllog";
47    private static final Log LOG = LogFactory.getLog(LogRollMasterProcedureManager.class);
48  
49    private MasterServices master;
50    private ProcedureCoordinator coordinator;
51    private boolean done;
52  
53    @Override
54    public void stop(String why) {
55      LOG.info("stop: " + why);
56    }
57  
58    @Override
59    public boolean isStopped() {
60      return false;
61    }
62  
63    @Override
64    public void initialize(MasterServices master, MetricsMaster metricsMaster)
65        throws KeeperException, IOException, UnsupportedOperationException {
66      this.master = master;
67      this.done = false;
68  
69      // setup the default procedure coordinator
70      String name = master.getServerName().toString();
71      ThreadPoolExecutor tpool = ProcedureCoordinator.defaultPool(name, 1);
72      BaseCoordinatedStateManager coordManager =
73          (BaseCoordinatedStateManager) CoordinatedStateManagerFactory
74          .getCoordinatedStateManager(master.getConfiguration());
75      coordManager.initialize(master);
76  
77      ProcedureCoordinatorRpcs comms =
78          coordManager.getProcedureCoordinatorRpcs(getProcedureSignature(), name);
79  
80      this.coordinator = new ProcedureCoordinator(comms, tpool);
81    }
82  
83    @Override
84    public String getProcedureSignature() {
85      return ROLLLOG_PROCEDURE_SIGNATURE;
86    }
87  
88    @Override
89    public void execProcedure(ProcedureDescription desc) throws IOException {
90      this.done = false;
91      // start the process on the RS
92      ForeignExceptionDispatcher monitor = new ForeignExceptionDispatcher(desc.getInstance());
93      List<ServerName> serverNames = master.getServerManager().getOnlineServersList();
94      List<String> servers = new ArrayList<String>();
95      for (ServerName sn : serverNames) {
96        servers.add(sn.toString());
97      }
98      
99      List<NameStringPair> conf = desc.getConfigurationList();
100     byte[] data = new byte[0];
101     if(conf.size() > 0){
102       // Get backup root path
103       data = conf.get(0).getValue().getBytes();
104     }
105     Procedure proc = coordinator.startProcedure(monitor, desc.getInstance(), data, servers);
106     if (proc == null) {
107       String msg = "Failed to submit distributed procedure for '" + desc.getInstance() + "'";
108       LOG.error(msg);
109       throw new IOException(msg);
110     }
111 
112     try {
113       // wait for the procedure to complete. A timer thread is kicked off that should cancel this
114       // if it takes too long.
115       proc.waitForCompleted();
116       LOG.info("Done waiting - exec procedure for " + desc.getInstance());
117       LOG.info("Distributed roll log procedure is successful!");
118       this.done = true;
119     } catch (InterruptedException e) {
120       ForeignException ee =
121           new ForeignException("Interrupted while waiting for roll log procdure to finish", e);
122       monitor.receive(ee);
123       Thread.currentThread().interrupt();
124     } catch (ForeignException e) {
125       ForeignException ee =
126           new ForeignException("Exception while waiting for roll log procdure to finish", e);
127       monitor.receive(ee);
128     }
129     monitor.rethrowException();
130   }
131 
132   @Override
133   public boolean isProcedureDone(ProcedureDescription desc) throws IOException {
134     return done;
135   }
136 
137 }