RatisHelper.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.ratis;
  19. import org.apache.hadoop.hdfs.protocol.DatanodeID;
  20. import org.apache.hadoop.scm.container.common.helpers.Pipeline;
  21. import org.apache.ratis.client.ClientFactory;
  22. import org.apache.ratis.client.RaftClient;
  23. import org.apache.ratis.conf.RaftProperties;
  24. import org.apache.ratis.protocol.RaftPeer;
  25. import org.apache.ratis.protocol.RaftPeerId;
  26. import org.apache.ratis.rpc.RpcType;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.List;
  32. import java.util.stream.Collectors;
  33. /**
  34. * Ratis helper methods.
  35. */
  36. public interface RatisHelper {
  37. Logger LOG = LoggerFactory.getLogger(RatisHelper.class);
  38. static String toRaftPeerIdString(DatanodeID id) {
  39. return id.getIpAddr() + ":" + id.getRatisPort();
  40. }
  41. static RaftPeerId toRaftPeerId(DatanodeID id) {
  42. return RaftPeerId.valueOf(toRaftPeerIdString(id));
  43. }
  44. static RaftPeer toRaftPeer(String id) {
  45. return new RaftPeer(RaftPeerId.valueOf(id), id);
  46. }
  47. static RaftPeer toRaftPeer(DatanodeID id) {
  48. return toRaftPeer(toRaftPeerIdString(id));
  49. }
  50. static List<RaftPeer> toRaftPeers(Pipeline pipeline) {
  51. return pipeline.getMachines().stream()
  52. .map(RatisHelper::toRaftPeer)
  53. .collect(Collectors.toList());
  54. }
  55. static RaftPeer[] toRaftPeerArray(Pipeline pipeline) {
  56. return toRaftPeers(pipeline).toArray(RaftPeer.EMPTY_PEERS);
  57. }
  58. static RaftClient newRaftClient(RpcType rpcType, Pipeline pipeline) {
  59. return newRaftClient(rpcType, toRaftPeerId(pipeline.getLeader()),
  60. toRaftPeers(pipeline));
  61. }
  62. static RaftClient newRaftClient(RpcType rpcType, RaftPeer leader) {
  63. return newRaftClient(rpcType, leader.getId(),
  64. new ArrayList<>(Arrays.asList(leader)));
  65. }
  66. static RaftClient newRaftClient(
  67. RpcType rpcType, RaftPeerId leader, List<RaftPeer> peers) {
  68. LOG.trace("newRaftClient: {}, leader={}, peers={}", rpcType, leader, peers);
  69. final RaftProperties properties = new RaftProperties();
  70. final ClientFactory factory = ClientFactory.cast(rpcType.newFactory(
  71. properties, null));
  72. return RaftClient.newBuilder()
  73. .setClientRpc(factory.newRaftClientRpc())
  74. .setServers(peers)
  75. .setLeaderId(leader)
  76. .setProperties(properties)
  77. .build();
  78. }
  79. }