From 932226f42ddca382cbf6c96eee79d5c48ae04f9b Mon Sep 17 00:00:00 2001 From: Mengyi Zhou Date: Mon, 24 Oct 2016 15:19:06 +0800 Subject: [PATCH] support slb-ids migration by 3 modes --- .../restful/message/view/ExtendedView.java | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/ctrip/zeus/restful/message/view/ExtendedView.java b/src/main/java/com/ctrip/zeus/restful/message/view/ExtendedView.java index 509b5425..d63c033d 100644 --- a/src/main/java/com/ctrip/zeus/restful/message/view/ExtendedView.java +++ b/src/main/java/com/ctrip/zeus/restful/message/view/ExtendedView.java @@ -4,8 +4,8 @@ import com.ctrip.zeus.model.entity.*; import com.ctrip.zeus.tag.entity.Property; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonView; -import com.netflix.config.DynamicBooleanProperty; import com.netflix.config.DynamicPropertyFactory; +import com.netflix.config.DynamicStringProperty; import java.util.List; @@ -123,7 +123,7 @@ public interface ExtendedView { } class ExtendedVs extends VsView implements ExtendedView { - private static DynamicBooleanProperty n2nViewEnabled = DynamicPropertyFactory.getInstance().getBooleanProperty("slb.slb-vs-n2n.view.enabled", false); + private static DynamicStringProperty n2nViewMode = DynamicPropertyFactory.getInstance().getStringProperty("slb.slb-vs-n2n.view.mode", "singular"); private List tags; private List properties; @@ -154,25 +154,31 @@ public interface ExtendedView { @Override Long getSlbId() { - if (n2nViewEnabled.get()) { - return null; - } else { - if (instance.getSlbId() == null && instance.getSlbIds().size() > 0) { - instance.setSlbId(instance.getSlbIds().get(0)); - } - return instance.getSlbId(); + switch (n2nViewMode.get()) { + case "plural": + return null; + case "singular": + case "redundant": + default: + if (instance.getSlbId() == null && instance.getSlbIds().size() > 0) { + instance.setSlbId(instance.getSlbIds().get(0)); + } + return instance.getSlbId(); } } @Override List getSlbIds() { - if (n2nViewEnabled.get()) { - if (instance.getSlbIds().size() == 0 && instance.getSlbId() != null) { - instance.getSlbIds().add(instance.getSlbId()); - } - return instance.getSlbIds(); - } else { - return null; + switch (n2nViewMode.get()) { + case "plural": + case "redundant": + if (instance.getSlbIds().size() == 0 && instance.getSlbId() != null) { + instance.getSlbIds().add(instance.getSlbId()); + } + return instance.getSlbIds(); + case "singular": + default: + return null; } } @@ -217,16 +223,26 @@ public interface ExtendedView { } public static void renderVirtualServer(VirtualServer vs) { - if (n2nViewEnabled.get()) { - if (vs.getSlbIds().size() == 0 && vs.getSlbId() != null) { - vs.getSlbIds().add(vs.getSlbId()); - } - vs.setSlbId(null); - } else { - if (vs.getSlbId() == null && vs.getSlbIds().size() > 0) { - vs.setSlbId(vs.getSlbIds().get(0)); - } - vs.getSlbIds().clear(); + switch (n2nViewMode.get()) { + case "plural": + if (vs.getSlbIds().size() == 0 && vs.getSlbId() != null) { + vs.getSlbIds().add(vs.getSlbId()); + } + vs.setSlbId(null); + break; + case "redundant": + if (vs.getSlbIds().size() == 0 && vs.getSlbId() != null) { + vs.getSlbIds().add(vs.getSlbId()); + } else if (vs.getSlbId() == null && vs.getSlbIds().size() > 0) { + vs.setSlbId(vs.getSlbIds().get(0)); + } + break; + case "singular": + default: + if (vs.getSlbId() == null && vs.getSlbIds().size() > 0) { + vs.setSlbId(vs.getSlbIds().get(0)); + } + vs.getSlbIds().clear(); } } }