1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.master;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.hadoop.hbase.HConstants;
25 import org.apache.hadoop.hbase.HRegionInfo;
26 import org.apache.hadoop.hbase.client.Delete;
27 import org.apache.hadoop.hbase.client.Get;
28 import org.apache.hadoop.hbase.client.Put;
29 import org.apache.hadoop.hbase.client.Result;
30 import org.apache.hadoop.hbase.ipc.HRegionInterface;
31 import org.apache.hadoop.hbase.util.Writables;
32
33 import java.io.IOException;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.TreeMap;
37
38
39
40
41 class ChangeTableState extends TableOperation {
42 private final Log LOG = LogFactory.getLog(this.getClass());
43 private boolean online;
44
45 protected final Map<String, HashSet<HRegionInfo>> servedRegions =
46 new TreeMap<String, HashSet<HRegionInfo>>();
47 protected long lockid;
48
49 ChangeTableState(final HMaster master, final byte [] tableName,
50 final boolean onLine)
51 throws IOException {
52 super(master, tableName);
53 this.online = onLine;
54 }
55
56 @Override
57 protected void processScanItem(String serverName, HRegionInfo info) {
58 if (isBeingServed(serverName)) {
59 HashSet<HRegionInfo> regions = this.servedRegions.get(serverName);
60 if (regions == null) {
61 regions = new HashSet<HRegionInfo>();
62 }
63 regions.add(info);
64 this.servedRegions.put(serverName, regions);
65 }
66 }
67
68 @Override
69 protected void postProcessMeta(MetaRegion m, HRegionInterface server)
70 throws IOException {
71
72 if (LOG.isDebugEnabled()) {
73 LOG.debug("Processing unserved regions");
74 }
75 for (HRegionInfo i: this.unservedRegions) {
76 if (i.isOffline() && i.isSplit()) {
77 if (LOG.isDebugEnabled()) {
78 LOG.debug("Skipping region " + i.toString() +
79 " because it is offline and split");
80 }
81 continue;
82 }
83
84 if(!this.online && this.master.getRegionManager().
85 isPendingOpen(i.getRegionNameAsString())) {
86 LOG.debug("Skipping region " + i.toString() +
87 " because it is pending open, will tell it to close later");
88 continue;
89 }
90
91
92
93 if (!(i.isOffline() && !online) && !(!i.isOffline() && online)) {
94
95 Put put = updateRegionInfo(i);
96 server.put(m.getRegionName(), put);
97 Delete delete = new Delete(i.getRegionName());
98 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
99 delete.deleteColumns(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER);
100 server.delete(m.getRegionName(), delete);
101 }
102 if (LOG.isDebugEnabled()) {
103 LOG.debug("Removed server and startcode from row and set online=" +
104 this.online + ": " + i.getRegionNameAsString());
105 }
106 synchronized (master.getRegionManager()) {
107 if (this.online) {
108
109 if (!this.master.getRegionManager().regionIsOpening(i.getRegionNameAsString())) {
110 this.master.getRegionManager().setUnassigned(i, false);
111 }
112 } else {
113
114 this.master.getRegionManager().removeRegion(i);
115 }
116 }
117 }
118
119
120 if (LOG.isDebugEnabled()) {
121 LOG.debug("Processing regions currently being served");
122 }
123 synchronized (this.master.getRegionManager()) {
124 for (Map.Entry<String, HashSet<HRegionInfo>> e:
125 this.servedRegions.entrySet()) {
126 String serverName = e.getKey();
127 if (this.online) {
128 LOG.debug("Already online");
129 continue;
130 }
131
132
133 for (HRegionInfo i: e.getValue()) {
134
135 Get get = new Get(i.getRegionName());
136 get.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
137 Result values = server.get(m.getRegionName(), get);
138 String serverAddress = BaseScanner.getServerAddress(values);
139
140 if(serverAddress.length() == 0) {
141 continue;
142 }
143 if (LOG.isDebugEnabled()) {
144 LOG.debug("Adding region " + i.getRegionNameAsString() +
145 " to setClosing list");
146 }
147
148 this.master.getRegionManager().setClosing(serverName, i, true);
149 }
150 }
151 }
152 this.servedRegions.clear();
153 }
154
155 protected Put updateRegionInfo(final HRegionInfo i)
156 throws IOException {
157 i.setOffline(!online);
158 Put put = new Put(i.getRegionName());
159 put.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER, Writables.getBytes(i));
160 return put;
161 }
162 }