1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
public class ZooKeeperMetaManager extends AbstractCanalLifeCycle implements CanalMetaManager {
private static final String ENCODE = "UTF-8"; private ZkClientx zkClientx;
public void start() { super.start();
Assert.notNull(zkClientx); }
public void stop() { zkClientx = null; super.stop(); }
public void subscribe(ClientIdentity clientIdentity) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getClientIdNodePath(clientIdentity.getDestination(), clientIdentity.getClientId());
try { zkClientx.createPersistent(path, true); } catch (ZkNodeExistsException e) { } if (clientIdentity.hasFilter()) { String filterPath = ZookeeperPathUtils.getFilterPath(clientIdentity.getDestination(), clientIdentity.getClientId());
byte[] bytes = null; try { bytes = clientIdentity.getFilter().getBytes(ENCODE); } catch (UnsupportedEncodingException e) { throw new CanalMetaManagerException(e); }
try { zkClientx.createPersistent(filterPath, bytes); } catch (ZkNodeExistsException e) { zkClientx.writeData(filterPath, bytes); } } }
public boolean hasSubscribe(ClientIdentity clientIdentity) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getClientIdNodePath(clientIdentity.getDestination(), clientIdentity.getClientId()); return zkClientx.exists(path); }
public void unsubscribe(ClientIdentity clientIdentity) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getClientIdNodePath(clientIdentity.getDestination(), clientIdentity.getClientId()); zkClientx.deleteRecursive(path); }
public List<ClientIdentity> listAllSubscribeInfo(String destination) throws CanalMetaManagerException { if (zkClientx == null) { return new ArrayList<>(); } String path = ZookeeperPathUtils.getDestinationPath(destination); List<String> childs = null; try { childs = zkClientx.getChildren(path); } catch (ZkNoNodeException e) { }
if (CollectionUtils.isEmpty(childs)) { return new ArrayList<>(); } List<Short> clientIds = new ArrayList<>(); for (String child : childs) { if (StringUtils.isNumeric(child)) { clientIds.add(ZookeeperPathUtils.getClientId(child)); } }
Collections.sort(clientIds); List<ClientIdentity> clientIdentities = Lists.newArrayList(); for (Short clientId : clientIds) { path = ZookeeperPathUtils.getFilterPath(destination, clientId); byte[] bytes = zkClientx.readData(path, true); String filter = null; if (bytes != null) { try { filter = new String(bytes, ENCODE); } catch (UnsupportedEncodingException e) { throw new CanalMetaManagerException(e); } } clientIdentities.add(new ClientIdentity(destination, clientId, filter)); }
return clientIdentities; }
public Position getCursor(ClientIdentity clientIdentity) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getCursorPath(clientIdentity.getDestination(), clientIdentity.getClientId());
byte[] data = zkClientx.readData(path, true); if (data == null || data.length == 0) { return null; }
return JsonUtils.unmarshalFromByte(data, Position.class); }
public void updateCursor(ClientIdentity clientIdentity, Position position) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getCursorPath(clientIdentity.getDestination(), clientIdentity.getClientId()); byte[] data = JsonUtils.marshalToByte(position, JSONWriter.Feature.WriteClassName); try { zkClientx.writeData(path, data); } catch (ZkNoNodeException e) { zkClientx.createPersistent(path, data, true); } }
public Long addBatch(ClientIdentity clientIdentity, PositionRange positionRange) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); byte[] data = JsonUtils.marshalToByte(positionRange, JSONWriter.Feature.WriteClassName); String batchPath = zkClientx .createPersistentSequential(path + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR, data, true); String batchIdString = StringUtils.substringAfterLast(batchPath, ZookeeperPathUtils.ZOOKEEPER_SEPARATOR); return ZookeeperPathUtils.getBatchMarkId(batchIdString); }
public void addBatch(ClientIdentity clientIdentity, PositionRange positionRange, Long batchId) throws CanalMetaManagerException { String path = ZookeeperPathUtils .getBatchMarkWithIdPath(clientIdentity.getDestination(), clientIdentity.getClientId(), batchId); byte[] data = JsonUtils.marshalToByte(positionRange, JSONWriter.Feature.WriteClassName); zkClientx.createPersistent(path, data, true); }
public PositionRange removeBatch(ClientIdentity clientIdentity, Long batchId) throws CanalMetaManagerException { String batchsPath = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); List<String> nodes = zkClientx.getChildren(batchsPath); if (CollectionUtils.isEmpty(nodes)) { return null; }
ArrayList<Long> batchIds = new ArrayList<>(nodes.size()); for (String batchIdString : nodes) { batchIds.add(Long.valueOf(batchIdString)); } Long minBatchId = Collections.min(batchIds); if (!minBatchId.equals(batchId)) { throw new CanalMetaManagerException(String.format("batchId:%d is not the firstly:%d", batchId, minBatchId)); }
if (!batchIds.contains(batchId)) { return null; } PositionRange positionRange = getBatch(clientIdentity, batchId); if (positionRange != null) { String path = ZookeeperPathUtils .getBatchMarkWithIdPath(clientIdentity.getDestination(), clientIdentity.getClientId(), batchId); zkClientx.delete(path); }
return positionRange; }
public PositionRange getBatch(ClientIdentity clientIdentity, Long batchId) throws CanalMetaManagerException { String path = ZookeeperPathUtils .getBatchMarkWithIdPath(clientIdentity.getDestination(), clientIdentity.getClientId(), batchId); byte[] data = zkClientx.readData(path, true); if (data == null) { return null; }
PositionRange positionRange = JsonUtils.unmarshalFromByte(data, PositionRange.class); return positionRange; }
public void clearAllBatchs(ClientIdentity clientIdentity) throws CanalMetaManagerException { String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); List<String> batchChilds = zkClientx.getChildren(path);
for (String batchChild : batchChilds) { String batchPath = path + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR + batchChild; zkClientx.delete(batchPath); } }
public PositionRange getLastestBatch(ClientIdentity clientIdentity) { String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); List<String> nodes = null; try { nodes = zkClientx.getChildren(path); } catch (ZkNoNodeException e) { }
if (CollectionUtils.isEmpty(nodes)) { return null; } ArrayList<Long> batchIds = new ArrayList<>(nodes.size()); for (String batchIdString : nodes) { batchIds.add(Long.valueOf(batchIdString)); } Long maxBatchId = Collections.max(batchIds); PositionRange result = getBatch(clientIdentity, maxBatchId); if (result == null) { return getLastestBatch(clientIdentity); } else { return result; } }
public PositionRange getFirstBatch(ClientIdentity clientIdentity) { String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); List<String> nodes = null; try { nodes = zkClientx.getChildren(path); } catch (ZkNoNodeException e) { }
if (CollectionUtils.isEmpty(nodes)) { return null; } ArrayList<Long> batchIds = new ArrayList<>(nodes.size()); for (String batchIdString : nodes) { batchIds.add(Long.valueOf(batchIdString)); } Long minBatchId = Collections.min(batchIds); PositionRange result = getBatch(clientIdentity, minBatchId); if (result == null) { return getFirstBatch(clientIdentity); } else { return result; } }
public Map<Long, PositionRange> listAllBatchs(ClientIdentity clientIdentity) { String path = ZookeeperPathUtils.getBatchMarkPath(clientIdentity.getDestination(), clientIdentity.getClientId()); List<String> nodes = null; try { nodes = zkClientx.getChildren(path); } catch (ZkNoNodeException e) { }
if (CollectionUtils.isEmpty(nodes)) { return Maps.newHashMap(); } ArrayList<Long> batchIds = new ArrayList<>(nodes.size()); for (String batchIdString : nodes) { batchIds.add(Long.valueOf(batchIdString)); }
Collections.sort(batchIds); Map<Long, PositionRange> positionRanges = Maps.newLinkedHashMap(); for (Long batchId : batchIds) { PositionRange result = getBatch(clientIdentity, batchId); if (result == null) { return listAllBatchs(clientIdentity); } else { positionRanges.put(batchId, result); } }
return positionRanges; }
public void setZkClientx(ZkClientx zkClientx) { this.zkClientx = zkClientx; }
}
|