gpt4 book ai didi

java - 如何为每个数据中心选择备用机器?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:33:54 24 4
gpt4 key购买 nike

给定的数据中心列表是 phx、slc、lvs 和机器列表、h0、h1、h2、h3,如下所述 -

Datacenters = phx, slc, lvs
Machines = h0, h1, h2, h3

我只想生成以下组合 -

a)   phx - h0
slc - h1
lvs - h2

b) phx - h1
slc - h2
lvs - h3

c) phx - h2
slc - h3
lvs - h0

d) phx - h3
slc - h0
lvs - h1

我需要调用 getDatacenterMachineMapping 方法,它会给出上述结果。对于第一次调用,它应该在 coloHost 映射中给我上面的 a 结果,在第二次调用中,它应该给我上面的 b 结果coloHost 映射中,在第三次调用中,它应该在 coloHost 映射中给我上面的 c 结果,对于第四次调用,它应该在 coloHost map 和第五次调用中给我上面的 d 结果,它不应该给我任何东西,因为没有留下任何组合。

private static List<String> colo = Arrays.asList("phx", "slc", "lvs");
private static List<String> hosts = Arrays.asList("h0","h1","h2", "h3");

while(true) {
Map<String, String> coloHost = getDatacenterMachineMapping();
System.out.println(coloHost);
}

private static Map<String, String> getDatacenterMachineMapping() {

// some code here?

}

每个 channel 中的每个数据中心都应该有备用机器/主机。他们不应该得到相同的机器。例如在 a 中,如上所示 - phx 获取 h0slc 获取 h1lvs 获取 h2 所以所有每个数据中心的机器都不同。在第二遍中,如 b 所示 - 现在 phx 得到 h1(因为 phx 在第一遍中已经得到 h0),slc 得到 h2 (bcoz slc 在第一遍中已经得到 h1),并且 lvs 得到了 h3(bcoz lvs 在第一遍中已经得到了 h2)等等。

还有一个例子——如果我只有三个主机,那么下面的组合我应该只得到——

Datacenters = phx, slc, lvs
Machines = h0, h1, h2

a) phx - h0
slc - h1
lvs - h2

b) phx - h1
slc - h2
lvs - h0

c) phx - h2
slc - h0
lvs - h1

有什么想法可以做到这一点吗?

更新:-

我试过你下面的代码,它工作正常。我在我的 main 方法中使用无限 while 循环,这意味着它会一直调用 getDatacenterMachineMapping 方法。所以这是我得到的输出,看起来不错。

{phx=h0, slc=h1, vs=h2}
{phx=h1, slc=h2, vs=h3}
{phx=h2, slc=h3, vs=h0}
{phx=h3, slc=h0, vs=h1}

{phx=h0, slc=h1, vs=h2}
{phx=h1, slc=h2, vs=h3}
{phx=h2, slc=h3, vs=h0}

因此,如果您查看上面打印出来的映射。前四个是我期望的唯一结果,之后我不希望 getDatacenterMachineMapping 给出任何结果,因为那样一次,phx 的所有四台机器都被打印出来,slc 的所有四台机器都被打印出来,vs 的所有四台机器都被打印出来。如果您看到第五行,它又开始了,我不希望 getDatacenterMachineMapping 给出结果。

下面是我试过的代码-

    public static void main(String[] args) {

DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1", "h2", "h3"));

while(true) {
Map<String, String> m1 = dcm.getDatacenterMachineMapping();
System.out.println(m1);
}
}

class DatacenterMachineMapping {

private boolean firstCall = true;
private int hostListIndex = 0;
private List<String> datacenterList, hostList;

public DatacenterMachineMapping(List<String> datacenterList, List<String> hostList) {
this.datacenterList = datacenterList;
this.hostList = hostList;
}

public Map<String, String> getDatacenterMachineMapping() {
Map<String, String> datacenterMachineMapping = new HashMap<String, String>();
if(! firstCall) {
if(hostListIndex == 0) {
hostListIndex = hostList.size();
}
hostListIndex--;
} else {
firstCall = false;
}
for(String datacenter : datacenterList) {
if(hostListIndex == hostList.size()) {
hostListIndex = 0;
}
datacenterMachineMapping.put(datacenter, hostList.get(hostListIndex++));
}
hostListIndex--;
return datacenterMachineMapping;
}
}

最佳答案

我可以从主机索引中看到的模式是,对 getDatacenterMachineMapping 的每个后续调用都从索引 i-1 开始,其中 i 是上一次调用的最后一个主机的索引。第一个调用从索引 0 开始,并围绕可用主机轮换。

例如,第一个主机列表:h0, h1, h2, h3

你有

1) 0=h0, 1=h1, 2=h2

最后一个索引是 2,从中减去 1 -> 1 这是下一次调用应该开始的索引:

2) 1=h1, 2=h2, 3=h3

现在最后一个索引是 3,即你在 2 开始下一个调用,这在列表中只剩下两个主机,然后你必须轮换,即返回索引 0 同样,这给了你

3) 2=h2, 3=h3, 0=h0

第二个主机列表也是如此。

这里是上面的示例实现(可能需要进行调整以完全符合您的要求,例如检查数据中心是否已经有任何可能的主机,您可以将其与 set 一起包装到自己的类中已分配主机的 (HashSet):

public class DatacenterMachineMapping {

private boolean firstCall = true;
private int hostListIndex = 0;
private List<String> datacenterList, hostList;

public DatacenterMachineMapping(List<String> datacenterList, List<String> hostList) {
this.datacenterList = datacenterList;
this.hostList = hostList;
}

public Map<String, String> getDatacenterMachineMapping() {
Map<String, String> datacenterMachineMapping = new HashMap<>();
if(! firstCall) {
if(hostListIndex == 0) {
hostListIndex = hostList.size();
}
hostListIndex--;
} else {
firstCall = false;
}
for(String datacenter : datacenterList) {
if(hostListIndex == hostList.size()) {
hostListIndex = 0;
}
datacenterMachineMapping.put(datacenter, hostList.get(hostListIndex++));
}
hostListIndex--;
return datacenterMachineMapping;
}
}

还有一些测试...

public class DatacenterMachineMappingTest {

@Test
public void test1() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1", "h2", "h3"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h2", m1.get("vs"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m2.get("phx"));
Assert.assertEquals("h2", m2.get("slc"));
Assert.assertEquals("h3", m2.get("vs"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h2", m3.get("phx"));
Assert.assertEquals("h3", m3.get("slc"));
Assert.assertEquals("h0", m3.get("vs"));
}

@Test
public void test2() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1", "h2"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h2", m1.get("vs"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m2.get("phx"));
Assert.assertEquals("h2", m2.get("slc"));
Assert.assertEquals("h0", m2.get("vs"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h2", m3.get("phx"));
Assert.assertEquals("h0", m3.get("slc"));
Assert.assertEquals("h1", m3.get("vs"));
}

@Test
public void test3() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h0", m1.get("vs"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m2.get("phx"));
Assert.assertEquals("h0", m2.get("slc"));
Assert.assertEquals("h1", m2.get("vs"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m3.get("phx"));
Assert.assertEquals("h1", m3.get("slc"));
Assert.assertEquals("h0", m3.get("vs"));
}

@Test
public void test4() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1", "h2", "h3", "h4", "h5", "h6"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h2", m1.get("vs"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m2.get("phx"));
Assert.assertEquals("h2", m2.get("slc"));
Assert.assertEquals("h3", m2.get("vs"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h2", m3.get("phx"));
Assert.assertEquals("h3", m3.get("slc"));
Assert.assertEquals("h4", m3.get("vs"));

Map<String, String> m4 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h3", m4.get("phx"));
Assert.assertEquals("h4", m4.get("slc"));
Assert.assertEquals("h5", m4.get("vs"));

Map<String, String> m5 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h4", m5.get("phx"));
Assert.assertEquals("h5", m5.get("slc"));
Assert.assertEquals("h6", m5.get("vs"));

Map<String, String> m6 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h5", m6.get("phx"));
Assert.assertEquals("h6", m6.get("slc"));
Assert.assertEquals("h0", m6.get("vs"));

Map<String, String> m7 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h6", m7.get("phx"));
Assert.assertEquals("h0", m7.get("slc"));
Assert.assertEquals("h1", m7.get("vs"));
}

@Test
public void test5() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs", "sls"),
Arrays.asList("h0", "h1", "h2", "h3", "h4", "h5", "h6"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h2", m1.get("vs"));
Assert.assertEquals("h3", m1.get("sls"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h2", m2.get("phx"));
Assert.assertEquals("h3", m2.get("slc"));
Assert.assertEquals("h4", m2.get("vs"));
Assert.assertEquals("h5", m2.get("sls"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h4", m3.get("phx"));
Assert.assertEquals("h5", m3.get("slc"));
Assert.assertEquals("h6", m3.get("vs"));
Assert.assertEquals("h0", m3.get("sls"));

Map<String, String> m4 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h6", m4.get("phx"));
Assert.assertEquals("h0", m4.get("slc"));
Assert.assertEquals("h1", m4.get("vs"));
Assert.assertEquals("h2", m4.get("sls"));

Map<String, String> m5 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m5.get("phx"));
Assert.assertEquals("h2", m5.get("slc"));
Assert.assertEquals("h3", m5.get("vs"));
Assert.assertEquals("h4", m5.get("sls"));

Map<String, String> m6 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h3", m6.get("phx"));
Assert.assertEquals("h4", m6.get("slc"));
Assert.assertEquals("h5", m6.get("vs"));
Assert.assertEquals("h6", m6.get("sls"));

Map<String, String> m7 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h5", m7.get("phx"));
Assert.assertEquals("h6", m7.get("slc"));
Assert.assertEquals("h0", m7.get("vs"));
Assert.assertEquals("h1", m7.get("sls"));
}
}

编辑:

添加一个映射,用于映射每个数据中心及其已接收的主机:

private Map<String, Set<String>> dataCenterHostsMap = new HashMap<>();

现在添加一个将主机添加到数据中心的条件:只有在主机尚未添加时才可以添加主机:

if(addDataCenterHost(datacenter, hostList.get(hostListIndex))) {
datacenterMachineMapping.put(datacenter, hostList.get(hostListIndex++));
}

addDataCenterHost 方法如下所示:

private boolean addDataCenterHost(String datacenter, String host) {
Set<String> dataCenterHostSet = dataCenterHostsMap.get(datacenter);
if(dataCenterHostSet == null) {
dataCenterHostSet = new HashSet<String>();
dataCenterHostsMap.put(datacenter, dataCenterHostSet);
}
return dataCenterHostSet.add(host);
}

随着这个改变 test3 将不再成功运行,我想你知道为什么 ;-) 。

这里是一个更改后的 test1,用于测试第 5 次调用。

@Test
public void test1() {
DatacenterMachineMapping dcm = new DatacenterMachineMapping(
Arrays.asList("phx", "slc", "vs"),
Arrays.asList("h0", "h1", "h2", "h3"));

Map<String, String> m1 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h0", m1.get("phx"));
Assert.assertEquals("h1", m1.get("slc"));
Assert.assertEquals("h2", m1.get("vs"));

Map<String, String> m2 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h1", m2.get("phx"));
Assert.assertEquals("h2", m2.get("slc"));
Assert.assertEquals("h3", m2.get("vs"));

Map<String, String> m3 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h2", m3.get("phx"));
Assert.assertEquals("h3", m3.get("slc"));
Assert.assertEquals("h0", m3.get("vs"));

Map<String, String> m4 = dcm.getDatacenterMachineMapping();
Assert.assertEquals("h3", m4.get("phx"));
Assert.assertEquals("h0", m4.get("slc"));
Assert.assertEquals("h1", m4.get("vs"));

Map<String, String> m5 = dcm.getDatacenterMachineMapping();
Assert.assertNull(m5.get("phx"));
Assert.assertNull(m5.get("slc"));
Assert.assertNull(m5.get("vs"));
}

关于java - 如何为每个数据中心选择备用机器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20029840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com