001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
007: import com.tc.config.schema.builder.InstrumentedClassConfigBuilder;
008: import com.tc.config.schema.builder.LockConfigBuilder;
009: import com.tc.config.schema.builder.RootConfigBuilder;
010: import com.tc.config.schema.setup.FatalIllegalConfigurationChangeHandler;
011: import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
012: import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
013: import com.tc.config.schema.test.ApplicationConfigBuilder;
014: import com.tc.config.schema.test.DSOApplicationConfigBuilderImpl;
015: import com.tc.config.schema.test.HaConfigBuilder;
016: import com.tc.config.schema.test.InstrumentedClassConfigBuilderImpl;
017: import com.tc.config.schema.test.L2ConfigBuilder;
018: import com.tc.config.schema.test.L2SConfigBuilder;
019: import com.tc.config.schema.test.LockConfigBuilderImpl;
020: import com.tc.config.schema.test.RootConfigBuilderImpl;
021: import com.tc.config.schema.test.TerracottaConfigBuilder;
022: import com.tc.object.config.StandardDSOClientConfigHelperImpl;
023: import com.tc.util.Assert;
024: import com.tc.util.PortChooser;
025:
026: import java.io.File;
027: import java.io.FileOutputStream;
028: import java.io.PrintWriter;
029:
030: public class ResolveTwoActiveServersTest extends TransparentTestBase {
031: private static final int NODES = 2;
032:
033: // This work only for 2 servers right now
034: private static final int SERVERS = 2;
035: private final int[] dsoPorts = new int[SERVERS];
036: private final int[] jmxPorts = new int[SERVERS];
037: private final int[] l2GroupPorts = new int[SERVERS];
038: private final int[] proxyPorts = new int[SERVERS];
039: private final String[] serverNames = new String[SERVERS];
040: private String persistenceMode = L2ConfigBuilder.PERSISTENCE_MODE_TEMPORARY_SWAP_ONLY;
041: private File[] configFiles;
042:
043: public ResolveTwoActiveServersTest() {
044: // TODO: fix and enable
045: // With tribes, the proxies get by-passed. When we come up with a work around for this, modify this test accordingly
046: // and enable it.
047:
048: this .disableAllUntil("3001-01-01");
049: }
050:
051: protected Class getApplicationClass() {
052: return ResolveTwoActiveServersTestApp.class;
053: }
054:
055: public void setUp() throws Exception {
056: assertEquals(SERVERS, 2);
057:
058: setPorts();
059:
060: // to be used by external process servers
061: configFiles = new File[SERVERS];
062: for (int i = 0; i < SERVERS; i++) {
063: configFiles[i] = getTempFile("config-file-" + i + ".xml");
064: writeConfigFile(configFiles[i], i);
065: }
066:
067: TestTVSConfigurationSetupManagerFactory factory = new TestTVSConfigurationSetupManagerFactory(
068: TestTVSConfigurationSetupManagerFactory.MODE_DISTRIBUTED_CONFIG,
069: null, new FatalIllegalConfigurationChangeHandler());
070:
071: // to be used by in-process clients
072: setConfigFactory(factory);
073:
074: L1TVSConfigurationSetupManager manager = factory
075: .createL1TVSConfigurationSetupManager();
076: setUpForMultipleExternalProcesses(factory,
077: new StandardDSOClientConfigHelperImpl(manager),
078: dsoPorts, jmxPorts, l2GroupPorts, proxyPorts,
079: serverNames, configFiles);
080:
081: getTransparentAppConfig().setClientCount(NODES);
082: initializeTestRunner();
083:
084: // TransparentAppConfig cfg = getTransparentAppConfig();
085: // cfg.setAttribute(ServerCrashingAppBase.CONFIG_FILE, configFile.getAbsolutePath());
086: // cfg.setAttribute(ServerCrashingAppBase.PORT_NUMBER, String.valueOf(port));
087: // cfg.setAttribute(ServerCrashingAppBase.HOST_NAME, "localhost");
088: }
089:
090: private void setConfigFactory(
091: TestTVSConfigurationSetupManagerFactory factory) {
092: for (int i = 0; i < SERVERS; i++) {
093: factory.addServerToL1Config(null, dsoPorts[i], jmxPorts[i]);
094: }
095: }
096:
097: private void writeConfigFile(File configFile, int index) {
098: try {
099: int theOther = (index + 1) % 2;
100:
101: L2ConfigBuilder[] l2s = new L2ConfigBuilder[SERVERS];
102: for (int i = 0; i < SERVERS; i++) {
103: L2ConfigBuilder l2 = new L2ConfigBuilder();
104: l2.setData(getTempFile("data-" + i).getAbsolutePath());
105: l2.setLogs(getTempFile("logs-" + i).getAbsolutePath());
106: serverNames[i] = "server-" + i;
107: l2.setName(serverNames[i]);
108: l2.setDSOPort(dsoPorts[i]);
109: l2.setJMXPort(jmxPorts[i]);
110: l2.setPersistenceMode(persistenceMode);
111: l2s[i] = l2;
112: }
113: l2s[index].setL2GroupPort(l2GroupPorts[index]);
114: l2s[theOther].setL2GroupPort(proxyPorts[theOther]);
115:
116: HaConfigBuilder ha = new HaConfigBuilder();
117: ha
118: .setMode(HaConfigBuilder.HA_MODE_NETWORKED_ACTIVE_PASSIVE);
119:
120: L2SConfigBuilder l2sConfigbuilder = new L2SConfigBuilder();
121: l2sConfigbuilder.setL2s(l2s);
122: l2sConfigbuilder.setHa(ha);
123:
124: DSOApplicationConfigBuilderImpl appConfigBuilder = new DSOApplicationConfigBuilderImpl();
125:
126: InstrumentedClassConfigBuilder[] instrClasses = new InstrumentedClassConfigBuilder[] { new InstrumentedClassConfigBuilderImpl(
127: getApplicationClass()) };
128: appConfigBuilder.setInstrumentedClasses(instrClasses);
129:
130: LockConfigBuilder[] locks = new LockConfigBuilder[] { new LockConfigBuilderImpl(
131: LockConfigBuilder.TAG_AUTO_LOCK,
132: getApplicationClass(),
133: LockConfigBuilder.LEVEL_WRITE) };
134: appConfigBuilder.setLocks(locks);
135:
136: RootConfigBuilder[] roots = new RootConfigBuilder[] {
137: new RootConfigBuilderImpl(getApplicationClass(),
138: "barrier"),
139: new RootConfigBuilderImpl(getApplicationClass(),
140: "myArrayTestRoot") };
141: appConfigBuilder.setRoots(roots);
142:
143: ApplicationConfigBuilder app = ApplicationConfigBuilder
144: .newMinimalInstance();
145: app.setDSO(appConfigBuilder);
146:
147: TerracottaConfigBuilder cb = new TerracottaConfigBuilder();
148: cb.setServers(l2sConfigbuilder);
149: cb.setApplication(app);
150:
151: FileOutputStream fileOutputStream = new FileOutputStream(
152: configFile);
153: PrintWriter out = new PrintWriter((fileOutputStream));
154: out.println(cb.toString());
155: out.flush();
156: out.close();
157: } catch (Exception e) {
158: throw Assert.failure("Can't create config file", e);
159: }
160: }
161:
162: private void setPorts() {
163: PortChooser pc = new PortChooser();
164:
165: for (int i = 0; i < SERVERS; i++) {
166: int dsoPort = pc.chooseRandomPort();
167: while (!isUnusedPort(dsoPort)) {
168: dsoPort = pc.chooseRandomPort();
169: }
170: dsoPorts[i] = dsoPort;
171:
172: int jmxPort = pc.chooseRandomPort();
173: while (!isUnusedPort(jmxPort)) {
174: jmxPort = pc.chooseRandomPort();
175: }
176: jmxPorts[i] = jmxPort;
177:
178: int l2GroupPort = pc.chooseRandomPort();
179: while (!isUnusedPort(l2GroupPort)) {
180: l2GroupPort = pc.chooseRandomPort();
181: }
182: l2GroupPorts[i] = l2GroupPort;
183:
184: int proxyPort = pc.chooseRandomPort();
185: while (!isUnusedPort(proxyPort)) {
186: proxyPort = pc.chooseRandomPort();
187: }
188: proxyPorts[i] = proxyPort;
189: }
190: }
191:
192: // private void setPorts() {
193: // int base = 9500;
194: //
195: // for (int i = 0; i < SERVERS; i++) {
196: // dsoPorts[i] = base - (i * 1000) + 10;
197: // jmxPorts[i] = base - (i * 1000) + 20;
198: // l2GroupPorts[i] = base - (i * 1000) + 30;
199: // proxyPorts[i] = base - (i * 1000) + 40;
200: // }
201: // }
202:
203: private boolean isUnusedPort(int port) {
204: for (int i = 0; i < dsoPorts.length; i++) {
205: if (dsoPorts[i] == port) {
206: return false;
207: }
208: }
209: for (int i = 0; i < jmxPorts.length; i++) {
210: if (jmxPorts[i] == port) {
211: return false;
212: }
213: }
214: for (int i = 0; i < l2GroupPorts.length; i++) {
215: if (l2GroupPorts[i] == port) {
216: return false;
217: }
218: }
219: for (int i = 0; i < proxyPorts.length; i++) {
220: if (proxyPorts[i] == port) {
221: return false;
222: }
223: }
224: return true;
225: }
226: }
|