private final void get_status(final GearmanPacket packet, final ServerClient client) { /* * A client issues this to get status information for a submitted job. * * Arguments: * - Job handle that was given in JOB_CREATED packet. */ final byte[] jobHandle = packet.getArgumentData(0); assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { // Send unknown job STATUS_RES packet final byte[] unknown = new byte[] {'0'}; final GearmanPacket status_res = new GearmanPacket( Magic.RES, Type.STATUS_RES, jobHandle, unknown, unknown, unknown, unknown); client.sendPacket(status_res, null /*TODO*/); return; } final GearmanPacket status = job.createStatusResPacket(); client.sendPacket(status, null /*TODO*/); }
private final void option_req(final GearmanPacket packet, final ServerClient client) { /* * A client issues this to set an option for the connection in the * job server. Returns a OPTION_RES packet on success, or an ERROR * packet on failure. * * Arguments: * - Name of the option to set. Possibilities are: * "exceptions" - Forward WORK_EXCEPTION packets to the client. */ final byte[] option = packet.getArgumentData(0); assert option != null; final byte[] exceptions = new byte[] {'e', 'x', 'c', 'e', 'p', 't', 'i', 'o', 'n', 's'}; if (Arrays.equals(option, exceptions)) { // exceptions option client.setForwardsExceptions(true); client.sendPacket(ServerStaticPackets.OPTION_RES_EXCEPTIONS, null /*TODO*/); } else { // unknown option client.sendPacket(ServerStaticPackets.ERROR_UNKNOWN_OPTION, null /*TODO*/); } }
private final void text_maxqueue(final String[] args, final ServerClient client) { final byte[] funcName = args[1].getBytes(GearmanConstants.UTF_8); if (funcName == null) { client.sendPacket(ServerStaticPackets.TEXT_INCOMPLETE_ARGS, null /*TODO*/); return; } final ByteArray funcNameBA = new ByteArray(funcName); final String sizeStr = args[2]; if (sizeStr == null) { client.sendPacket(ServerStaticPackets.TEXT_INCOMPLETE_ARGS, null /*TODO*/); return; } final int size; try { size = Integer.parseInt(sizeStr); } catch (NumberFormatException e) { client.sendPacket(ServerStaticPackets.TEXT_OK, null /*TODO*/); return; } /* * FWIX CHANGE */ // final ServerFunction func = this.funcSet.getFunctionIfDefined(funcNameBA); funcSet.setMaxQueueByName(funcNameBA, size); }
private final void worker_time_status(final String[] args, final ServerClient client) { synchronized (this.server.getClientSet()) { for (ServerClient c : this.server.getClientSet()) client.sendPacket(c.getTimeStatus(), null /*TODO*/); } client.sendPacket(ServerStaticPackets.TEXT_DONE, null /*TODO*/); }
@Test(enabled = false) // TODO public void testStopAndStartServer() throws Exception { client.stopServer(testServerId); assertTrue(serverStatusChecker.apply(Server.State.STOPPED)); client.startServer(testServerId); assertTrue(serverStatusChecker.apply(Server.State.RUNNING)); }
/** {@inheritDoc} */ @Override public void onClientDisconnectes(Client data) { ServerClient sc = m_clients.getServerClientByClientInfo(data); if (sc != null) { synchronized (m_clients) { m_clients.removeClient(sc); } sc.tryClose(); } else { // log.warn("cannot find client " + data.m_id); } }
@Test public void testListServers() throws Exception { Set<Server> response = client.listServers(); assertNotNull(response); assertTrue(response.size() > 0); for (Server server : response) { ServerDetails newDetails = client.getServerDetails(server.getId()); assertEquals(newDetails.getId(), server.getId()); assertEquals(newDetails.getHostname(), server.getHostname()); assertEquals(newDetails.getPlatform(), server.getPlatform()); assertEquals(newDetails.getDatacenter(), server.getDatacenter()); checkServer(newDetails); } }
/** * triggered when an admin client connects * * @param data * @param connection */ public void onNewAdminClient(Client data, NetworkServiceClient connection) { // also trigger the normal client handling onNewClient(data, connection); ServerClient newAdminClient = new ServerClient(data, connection); synchronized (m_adminClients) { m_adminClients.put(data, newAdminClient); } // tell him about all the server configs + send him list of all clients List<ServerClient> all = null; synchronized (m_clients) { // copy the entries to be reistent to modifications while sending List<ServerClient> targets = m_clients.getAllClients(); all = new ArrayList<ServerClient>(targets); } String msg = ""; // tell him about all the clients that are connected for (ServerClient sc : all) { msg = AdminServerProtocolAbstractor.createClientMessage( sc.getClientInfo().m_id, sc.getClientInfo().m_address, true); sendToClient(newAdminClient, msg); } // tell him about all server configs: // TODO maybe add more configs! String pollTimeKey = Definitions.PREFIX_SERVER_DEFAULT_POLLING; String pollTimeValue = m_prefs.getValue(pollTimeKey); msg = AdminServerProtocolAbstractor.createConfigMessage( new Configuration( "default polling time: ", pollTimeKey, SettingType.number, pollTimeValue)); sendToClient(newAdminClient, msg); // send all invisible sensors as sensor-config items // + send all polling sensors as polling frequency objects List<Sensor> allSensors = Server.getSensorStorage().getAllSensors(); for (Sensor sensor : allSensors) { msg = createSensorVisiblityConf(sensor); sendToClient(newAdminClient, msg); if (sensor.isPolling) { msg = createSensorPollTimeConf(sensor); sendToClient(newAdminClient, msg); } } }
private final void work_status(final GearmanPacket packet, final ServerClient client) { /* * This is sent to update the server (and any listening clients) * of the status of a running job. The client should send these * periodically for long running jobs to update the percentage * complete. The job server should store this information so a client * who issued a background command may retrieve it later with a * GET_STATUS request. * * Arguments: * - NULL byte terminated job handle. * - NULL byte terminated percent complete numerator. * - Percent complete denominator. */ final byte[] jobHandle = packet.getArgumentData(0); assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final byte[] num = packet.getArgumentData(1); assert num != null; final byte[] den = packet.getArgumentData(2); assert den != null; final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/); } else { packet.setMagic(Magic.RES); job.setStatus(num, den); job.sendPacket(packet); } }
/** * Called when a text-based packet is received. When a packet with type -1 is received, it's * assumed to be a text-based packet. The first argument (The command argument in a text-based * packet) is checked against the set of known commands. Then, the command is executed or an * exception is returned to the client if the command is unknown. * * @param packet The received packet with a type of -1 * @param client The client who received the packet */ private final void text_packet(final GearmanPacket packet, final ServerClient client) { final String pkt = new String(packet.toBytes(), GearmanConstants.UTF_8); final String[] args = pkt.trim().split("[ \t]"); if (args[0].equalsIgnoreCase("workers")) { text_workers(args, client); return; } else if (args[0].equalsIgnoreCase("status")) { text_status(args, client); return; } else if (args[0].equalsIgnoreCase("maxqueue")) { text_maxqueue(args, client); return; } else if (args[0].equalsIgnoreCase("shutdown")) { text_shutdown(args, client); return; } else if (args[0].equalsIgnoreCase("version")) { text_version(args, client); return; } else if (args[0].equalsIgnoreCase("throttle")) { update_throttle(args, client); return; } else if (args[0].equalsIgnoreCase("memstats")) { getMemStats(client); return; } else if (args[0].equalsIgnoreCase("workertime")) { worker_time_status(args, client); return; } else { client.sendPacket(ServerStaticPackets.TEXT_UNKNOWN_COMMAND, null /*TODO*/); } }
private final void work_data(final GearmanPacket packet, final ServerClient client) { /* * This is sent to update the client with data from a running job. A * client should use this when it needs to send updates, send partial * results, or flush data during long running jobs. It can also be * used to break up a result so the client does not need to buffer * the entire result before sending in a WORK_COMPLETE packet. * * Arguments: * - NULL byte terminated job handle. * - Opaque data that is returned to the client. */ final byte[] jobHandle = packet.getArgumentData(0); assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/); } else { packet.setMagic(Magic.RES); job.sendPacket(packet); } }
@Test public void testConsole() throws Exception { Console console = client.getConsole(testServerId); assertNotNull(console); assertNotNull(console.getHost()); assertTrue(console.getPort() > 0 && console.getPort() < 65537); assertNotNull(console.getPassword()); }
@Test public void testListTemplates() throws Exception { Set<OSTemplate> oSTemplates = client.listTemplates(); for (OSTemplate oSTemplate : oSTemplates) { checkTemplate(oSTemplate); } }
private final void grab_job_uniq(final GearmanPacket packet, final ServerClient client) { /* * Just like GRAB_JOB, but return JOB_ASSIGN_UNIQ when there is a job. * * Arguments: * - None. */ client.grabJobUniq(); }
@Test public void testAllowedArguments() throws Exception { Map<String, AllowedArgumentsForCreateServer> templates = client.getAllowedArgumentsForCreateServerByPlatform(); assertTrue(templates.containsKey("OpenVZ")); assertTrue(templates.containsKey("Xen")); checkAllowedArguments(templates.get("OpenVZ")); checkAllowedArguments(templates.get("Xen")); }
@Test(enabled = false) // TODO work a better plan public void testRebootServer() throws Exception { long uptime = 0; while (uptime < 20) { uptime = client.getServerStatus(testServerId).getUptime().getCurrent(); } assertTrue(uptime > 19); client.rebootServer(testServerId); Thread.sleep(1000); uptime = client.getServerStatus(testServerId).getUptime().getCurrent(); assertTrue(uptime < 20); assertTrue(serverStatusChecker.apply(Server.State.RUNNING)); }
private final void reset_abilities(final GearmanPacket packet, final ServerClient client) { /* * This is sent to notify the server that the client is no longer * able to do any functions it previously registered with CAN_DO or * CAN_DO_TIMEOUT. * * Arguments: * - None. */ client.reset(); }
@Test public void testServerDetails() throws Exception { ServerDetails details = client.getServerDetails(testServerId); checkServer(details); assertEquals("Ubuntu 10.04 LTS 32-bit", details.getTemplateName()); assertEquals("Falkenberg", details.getDatacenter()); assertEquals("OpenVZ", details.getPlatform()); assertEquals(5, details.getDiskSizeGB()); assertEquals(512, details.getMemorySizeMB()); assertEquals(1, details.getCpuCores()); assertEquals(50, details.getTransferGB()); }
private final void grab_job(final GearmanPacket packet, final ServerClient client) { /* * This is sent to the server to request any available jobs on the * queue. The server will respond with either NO_JOB or JOB_ASSIGN, * depending on whether a job is available. * * Arguments: * - None. */ client.grabJob(); }
// takes a few minutes and requires an extra server (using 2 already) @Test(enabled = false) public void testCloneServer() throws Exception { ServerDetails testServer2 = client.cloneServer(testServerId, testHostName2, CloneServerOptions.Builder.cpucores(1)); assertNotNull(testServer2.getId()); assertEquals(testServer2.getHostname(), "jclouds-test2"); assertTrue(testServer2.getIps().isEmpty()); testServerId2 = testServer2.getId(); RetryablePredicate<Server.State> cloneChecker = new ServerStatusChecker(client, testServerId2, 300, 10, TimeUnit.SECONDS); assertTrue(cloneChecker.apply(Server.State.STOPPED)); client.startServer(testServer2.getId()); // TODO ServerStatus==STOPPED suggests the previous call to start should have worked cloneChecker = new RetryablePredicate<Server.State>( new Predicate<Server.State>() { public boolean apply(Server.State value) { ServerStatus status = client.getServerStatus(testServerId2, ServerStatusOptions.Builder.state()); if (status.getState() == value) { return true; } client.startServer(testServerId2); return false; } }, 300, 10, TimeUnit.SECONDS); assertTrue(cloneChecker.apply(Server.State.RUNNING)); }
private final void echo_req(final GearmanPacket packet, final ServerClient client) { /* * When a job server receives this request, it simply generates a * ECHO_RES packet with the data. This is primarily used for testing * or debugging. * * Arguments: * - Opaque data that is echoed back in response. */ packet.setMagic(Magic.RES); client.sendPacket(packet, null /*TODO*/); }
private void disconnect(int id, boolean status) { ServerClient c = null; boolean existed = false; for (int i = 0; i < clients.size(); i++) { if (clients.get(i).getID() == id) { c = clients.get(i); clients.remove(i); existed = true; break; } } if (!existed) return; String message = ""; if (status) { message = "Client " + c.name + " (" + c.getID() + ") @ " + c.address.toString() + ":" + c.port + " disconnected."; } else { message = "Client " + c.name + " (" + c.getID() + ") @ " + c.address.toString() + ":" + c.port + " timed out."; } System.out.println(message); }
private final void pre_sleep(final GearmanPacket packet, final ServerClient client) { /* * This is sent to notify the server that the client is about to * sleep, and that it should be woken up with a NOOP packet if a * job comes in for a function the client is able to perform. * * Arguments: * - None. */ // Places the client in sleep mode. This will essentially tell the client to // send a NOOP packet when it is notified of a Job coming in client.sleep(); }
public void getMemStats(final ServerClient client) { StringBuilder sb = new StringBuilder(); sb.append(Main.memUsed()); sb.append('\t'); sb.append(Main.getThreadCount()); sb.append('\t'); sb.append(Main.getActiveThreadCount()); sb.append('\t'); sb.append(Main.getUpTime()); sb.append('\n'); client.sendPacket(GearmanPacket.createTEXT(sb.toString()), null /*TODO*/); return; }
private final void work_complete(final GearmanPacket packet, final ServerClient client) { /* * This is to notify the server (and any listening clients) that * the job completed successfully. * * Arguments: * - NULL byte terminated job handle. * - Opaque data that is returned to the client as a response. */ final byte[] jobHandle = packet.getArgumentData(0); assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/); } else { // Construct a WORK_COMPLETE response packet client.jobCOmplete(); job.workComplete(packet); } }
@Test public void testServerLimits() throws Exception { Map<String, ServerLimit> limits = client.getServerLimits(testServerId); assertNotNull(limits); for (Map.Entry<String, ServerLimit> entry : limits.entrySet()) { assertNotNull(entry.getKey()); assertNotNull(entry.getValue()); ServerLimit limit = entry.getValue(); assertTrue(limit.getBarrier() >= 0); assertTrue(limit.getFailCount() == 0); assertTrue(limit.getHeld() >= 0); assertTrue(limit.getLimit() > 0); assertTrue(limit.getMaxHeld() >= 0); } }
/** * Called when a CANT_DO packet comes in.<br> * <br> * <i> This is sent to notify the server that the client is no longer able to perform the given * function.<br> * <br> * Arguments:<br> * - Function name.<br> * </i> * * @param packet The CANT_DO packet * @param client The client who aquired the packet */ private final void cant_do(final GearmanPacket packet, final ServerClient client) { /* * This is sent to notify the server that the client is no longer able to * perform the given function. * * Arguments: * - Function name. */ // Function Name final byte[] funcName = packet.getArgumentData(0); assert funcName != null; if (funcName.length == 0) { // TODO send error } client.cant_do(new ByteArray(funcName)); }
private final void work_fail(final GearmanPacket packet, final ServerClient client) { /* * This is to notify the server (and any listening clients) that * the job failed. * * Arguments: * - Job handle. */ final byte[] jobHandle = packet.getArgumentData(0); assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/); } else { job.workComplete(packet); } }
private final void set_client_id(final GearmanPacket packet, final ServerClient client) { /* * This sets the client ID in a job server so monitoring and reporting * commands can uniquely identify the various clients, and different * connections to job servers from the same client. * * Arguments: * - Unique string to identify the client instance. */ // Get Client ID from packet final byte[] clientIdBytes = packet.getArgumentData(0); assert clientIdBytes != null; // Convert the worker ID into a String final String clientId = new String(clientIdBytes, GearmanConstants.UTF_8); // Set the client's client ID client.setClientId(clientId); }
private final void work_exception(final GearmanPacket packet, final ServerClient client) { /* * This is to notify the server (and any listening clients) that * the job failed with the given exception. * * Arguments: * - NULL byte terminated job handle. * - Opaque data that is returned to the client as an exception. */ // Note: The protocol states this packet notifies the server that the specified job // has failed. However, the C server does not fail the Job. It is effectively a // WORK_WARNING packet that is only sent to clients that have specified they want // exceptions forwarded to them. This server will do the same as long as the C // server does so. /* * This is sent to update the client with data from a running job. A * client should use this when it needs to send updates, send partial * results, or flush data during long running jobs. It can also be * used to break up a result so the client does not need to buffer * the entire result before sending in a WORK_COMPLETE packet. * * Arguments: * - NULL byte terminated job handle. * - Opaque data that is returned to the client. */ final byte[] jobHandle = packet.getArgumentData(0); ; assert jobHandle != null; final ByteArray jobHandleBA = new ByteArray(jobHandle); final ServerJob job = ServerJobAbstract.getJob(jobHandleBA); if (job == null) { client.sendPacket(ServerStaticPackets.ERROR_JOB_NOT_FOUND, null /*TODO*/); } else { packet.setMagic(Magic.RES); job.sendPacket(packet); } }