ServerSelector { public DasResult execute(DasRequest dasRequest) throws TException { if (currentClient.get() != null) { DasService.Client client = currentClient.get().getClient(); return client.execute(dasRequest); } else { DasServerInstanceWithStatus chosen = rule.chooseServer(); if (chosen == null) { throw new RuntimeException("No available server from: " + serverInstances); } else { try { ClientObject co = servers.get(chosen).get(Thread.currentThread().getName()); return co.getClient().execute(dasRequest); } catch (Exception e){ try { servers.get(chosen).invalidate(Thread.currentThread().getName()); } catch (ExecutionException ee) {} throw new TException(e); } } } } ServerSelector(String appId, List<DasServerInstance> serverInstances, String dasClientVersion, String ppdaiClientVersion, String clientAddress); DasResult execute(DasRequest dasRequest); void commit(DasTransactionId transactionId); void rollback(DasTransactionId transactionId); DasTransactionId start(String appId, String database, DasHints hints); void stickServerMode(); void removeStick(); void startup(); List<DasServerInstanceWithStatus> getAllServers();  }
@Test public void testBasicCase() throws TException { ServerSelector serverSelector = new ServerSelector( "appId", Arrays.asList(AVAILABLE_INSTANCE_1, AVAILABLE_INSTANCE_2), "dasClientVersion", "ppdaiClientVersion", "clientAddress" ); DasResult response = serverSelector.execute(REQUEST); Assert.assertNotNull(response); }
@Test public void testRecover() throws TException, InterruptedException { ServerSelector serverSelector = new ServerSelector( "appId", Arrays.asList(NO_AVAILABLE_INSTANCE_1, NO_AVAILABLE_INSTANCE_2), "dasClientVersion", "ppdaiClientVersion", "clientAddress" ); try { serverSelector.execute(REQUEST); fail(); } catch (Exception e){ } serverSelector.serverInstances.add(AVAILABLE_INSTANCE_1); TimeUnit.SECONDS.sleep(ServerSelector.checkInterval + 1); DasResult response = serverSelector.execute(REQUEST); Assert.assertNotNull(response); }
@Test public void testAlwaysChooseAvailableServer() throws TException { ServerSelector serverSelector = new ServerSelector( "appId", Arrays.asList(AVAILABLE_INSTANCE_1, NO_AVAILABLE_INSTANCE_1), "dasClientVersion", "ppdaiClientVersion", "clientAddress" ); for (int i = 0 ;i < 100; i++){ DasResult response = serverSelector.execute(REQUEST); Assert.assertNotNull(response); } }
@Test(expected = RuntimeException.class) public void testNoAvailableInstance() throws TException { ServerSelector serverSelector = new ServerSelector( "appId", Arrays.asList(NO_AVAILABLE_INSTANCE_1, NO_AVAILABLE_INSTANCE_2), "dasClientVersion", "ppdaiClientVersion", "clientAddress" ); serverSelector.execute(REQUEST); fail(); }