Showing
1 changed file
with
16 additions
and
3 deletions
@@ -158,15 +158,28 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | @@ -158,15 +158,28 @@ public class YohoIndexServiceImpl implements IYohoIndexService, ApplicationEvent | ||
158 | } | 158 | } |
159 | // 获取配置的主分片数 | 159 | // 获取配置的主分片数 |
160 | int numberOfPrimary = MapUtils.getIntValue(indexSettings, "number_of_shards");//配置的主分片数 | 160 | int numberOfPrimary = MapUtils.getIntValue(indexSettings, "number_of_shards");//配置的主分片数 |
161 | - // 动态计算复制分片数【1个主分片就是机器数-1 , 4个主分片,4台机器就是 0,然后取1 】 | ||
162 | - int number_of_replicas = dataNodesCount/numberOfPrimary - 1; | ||
163 | - return Math.max(1, number_of_replicas); | 161 | + // 计算副本数 |
162 | + return getReplicasShardCount(dataNodesCount,numberOfPrimary); | ||
164 | } catch (Exception e) { | 163 | } catch (Exception e) { |
165 | INDEX_REBUILD_LOG.error(e.getMessage(), e); | 164 | INDEX_REBUILD_LOG.error(e.getMessage(), e); |
166 | return 0; | 165 | return 0; |
167 | } | 166 | } |
168 | } | 167 | } |
169 | 168 | ||
169 | + public static int getReplicasShardCount(int dataNodesCount, int numberOfPrimary){ | ||
170 | + if(dataNodesCount==numberOfPrimary){ | ||
171 | + return 1; | ||
172 | + } | ||
173 | + int maxTotalIndexCount = dataNodesCount ; | ||
174 | + int totalIndexCount = dataNodesCount * 2 / numberOfPrimary; | ||
175 | + totalIndexCount = Math.min(totalIndexCount, maxTotalIndexCount); | ||
176 | + return Math.max(2, totalIndexCount) -1; | ||
177 | + } | ||
178 | + | ||
179 | + public static void main(String[] args) { | ||
180 | + System.out.println(getReplicasShardCount(4,1)); | ||
181 | + } | ||
182 | + | ||
170 | /** | 183 | /** |
171 | * 创建索引,并返回真实索引名称 | 184 | * 创建索引,并返回真实索引名称 |
172 | * | 185 | * |
-
Please register or login to post a comment