You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by ki...@apache.org on 2012/10/25 01:14:57 UTC
[2/42] Refactoring the package names and removing jsql parser
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ControllerStatusUpdateResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ControllerStatusUpdateResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ControllerStatusUpdateResource.java
new file mode 100644
index 0000000..85d45db
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ControllerStatusUpdateResource.java
@@ -0,0 +1,117 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ControllerStatusUpdateResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ControllerStatusUpdateResource.class);
+
+ public ControllerStatusUpdateResource(Context context, Request request,
+ Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String zkServer = (String) getContext().getAttributes().get(
+ RestAdminApplication.ZKSERVERADDRESS);
+ String clusterName = (String) getRequest().getAttributes().get(
+ "clusterName");
+ String messageType = (String) getRequest().getAttributes().get(
+ "MessageType");
+ String messageId = (String) getRequest().getAttributes().get("MessageId");
+ // TODO: need pass sessionId to this represent()
+ String sessionId = (String) getRequest().getAttributes().get("SessionId");
+
+ presentation = getControllerStatusUpdateRepresentation(zkServer,
+ clusterName, sessionId, messageType, messageId);
+ } catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil
+ .getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getControllerStatusUpdateRepresentation(
+ String zkServerAddress, String clusterName, String sessionId,
+ String messageType, String messageId) throws JsonGenerationException,
+ JsonMappingException, IOException
+ {
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ String message = ClusterRepresentationUtil.getPropertyAsString(
+ zkClient,
+ clusterName,
+ keyBuilder.controllerTaskStatus(messageType, messageId),
+ MediaType.APPLICATION_JSON);
+ StringRepresentation representation = new StringRepresentation(message,
+ MediaType.APPLICATION_JSON);
+ return representation;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStateResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStateResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStateResource.java
new file mode 100644
index 0000000..062385b
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStateResource.java
@@ -0,0 +1,123 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class CurrentStateResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(CurrentStateResource.class);
+
+ public CurrentStateResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ String resourceGroup = (String) getRequest().getAttributes().get("resourceName");
+
+ presentation =
+ getInstanceCurrentStateRepresentation(
+ clusterName,
+ instanceName,
+ resourceGroup);
+ }
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstanceCurrentStateRepresentation(
+ String clusterName,
+ String instanceName,
+ String resourceGroup) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ ZkClient zkClient = (ZkClient)getRequest().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ String instanceSessionId =
+ ClusterRepresentationUtil.getInstanceSessionId(zkClient,
+ clusterName,
+ instanceName);
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+
+ String message =
+ ClusterRepresentationUtil.getInstancePropertyAsString(zkClient,
+ clusterName,
+ keyBuilder.currentState(instanceName,
+ instanceSessionId,
+ resourceGroup),
+ MediaType.APPLICATION_JSON);
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+ return representation;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStatesResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStatesResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStatesResource.java
new file mode 100644
index 0000000..4905bd9
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/CurrentStatesResource.java
@@ -0,0 +1,98 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyType;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class CurrentStatesResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(CurrentStatesResource.class);
+
+ public CurrentStatesResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ presentation = getInstanceCurrentStatesRepresentation(clusterName, instanceName);
+ }
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstanceCurrentStatesRepresentation(String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException
+ {
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
+ String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
+
+ String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
+
+ StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorResource.java
new file mode 100644
index 0000000..a1cc5c4
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorResource.java
@@ -0,0 +1,121 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ErrorResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ErrorResource.class);
+
+ public ErrorResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ String resourceGroup = (String) getRequest().getAttributes().get("resourceName");
+
+ presentation =
+ getInstanceErrorsRepresentation(clusterName,
+ instanceName,
+ resourceGroup);
+ }
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstanceErrorsRepresentation(String clusterName,
+ String instanceName,
+ String resourceGroup) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ String instanceSessionId =
+ ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName,
+ instanceName);
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ String message =
+ ClusterRepresentationUtil.getInstancePropertiesAsString(zkClient, clusterName,
+ keyBuilder.stateTransitionErrors(instanceName,
+ instanceSessionId,
+ resourceGroup),
+ // instanceSessionId
+ // + "__"
+ // + resourceGroup,
+ MediaType.APPLICATION_JSON);
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+ return representation;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorsResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorsResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorsResource.java
new file mode 100644
index 0000000..cf5e235
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ErrorsResource.java
@@ -0,0 +1,99 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyType;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ErrorsResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ErrorsResource.class);
+
+ public ErrorsResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ presentation = getInstanceErrorsRepresentation( clusterName, instanceName);
+ }
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstanceErrorsRepresentation(String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException
+ {
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
+
+ String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
+
+ String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
+
+ StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ExternalViewResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ExternalViewResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ExternalViewResource.java
new file mode 100644
index 0000000..0484f65
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ExternalViewResource.java
@@ -0,0 +1,111 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ExternalViewResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ExternalViewResource.class);
+
+ public ExternalViewResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceName = (String) getRequest().getAttributes().get("resourceName");
+ presentation = getExternalViewRepresentation( clusterName, resourceName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getExternalViewRepresentation(String clusterName,
+ String resourceName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);;
+
+ String message =
+ ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
+ clusterName,
+ keyBuilder.externalView(resourceName),
+ MediaType.APPLICATION_JSON);
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/IdealStateResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/IdealStateResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/IdealStateResource.java
new file mode 100644
index 0000000..9479b68
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/IdealStateResource.java
@@ -0,0 +1,195 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixException;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.IdealState;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class IdealStateResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(IdealStateResource.class);
+
+ public IdealStateResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceName = (String) getRequest().getAttributes().get("resourceName");
+ presentation = getIdealStateRepresentation(clusterName, resourceName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getIdealStateRepresentation(String clusterName, String resourceName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ String message =
+ ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
+ clusterName,
+ keyBuilder.idealStates(resourceName),
+ MediaType.APPLICATION_JSON);
+
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceName = (String) getRequest().getAttributes().get("resourceName");
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+
+ if (command.equalsIgnoreCase(ClusterSetup.addIdealState))
+ {
+ ZNRecord newIdealState = jsonParameters.getExtraParameter(JsonParameters.NEW_IDEAL_STATE);
+ HelixDataAccessor accessor =
+ ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
+
+ accessor.setProperty(accessor.keyBuilder().idealStates(resourceName),
+ new IdealState(newIdealState));
+
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.rebalance))
+ {
+ int replicas =
+ Integer.parseInt(jsonParameters.getParameter(JsonParameters.REPLICAS));
+ if (jsonParameters.getParameter(JsonParameters.RESOURCE_KEY_PREFIX) != null)
+ {
+ setupTool.rebalanceStorageCluster(clusterName,
+ resourceName,
+ replicas,
+ jsonParameters.getParameter(JsonParameters.RESOURCE_KEY_PREFIX));
+ }
+ else
+ {
+ setupTool.rebalanceStorageCluster(clusterName, resourceName, replicas);
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.expandResource))
+ {
+ setupTool.expandResource(clusterName, resourceName);
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.addResourceProperty))
+ {
+ Map<String, String> parameterMap = jsonParameters.cloneParameterMap();
+ parameterMap.remove(JsonParameters.MANAGEMENT_COMMAND);
+ for (String key : parameterMap.keySet())
+ {
+ setupTool.addResourceProperty(clusterName,
+ resourceName,
+ key,
+ parameterMap.get(key));
+ }
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.addIdealState + ", "
+ + ClusterSetup.rebalance + ", " + ClusterSetup.expandResource + ", "
+ + ClusterSetup.addResourceProperty + "]");
+ }
+
+ getResponse().setEntity(getIdealStateRepresentation(clusterName, resourceName));
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("Error in posting " + entity, e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstanceResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstanceResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstanceResource.java
new file mode 100644
index 0000000..3c7f389
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstanceResource.java
@@ -0,0 +1,230 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class InstanceResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(InstanceResource.class);
+
+ public InstanceResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return true;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ presentation = getInstanceRepresentation();
+ }
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstanceRepresentation() throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ String message =
+ ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
+ clusterName,
+ MediaType.APPLICATION_JSON,
+ keyBuilder.instanceConfig(instanceName));
+
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+ if (command.equalsIgnoreCase(ClusterSetup.enableInstance))
+ {
+ jsonParameters.verifyCommand(ClusterSetup.enableInstance);
+
+ boolean enabled =
+ Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.getClusterManagementTool().enableInstance(clusterName,
+ instanceName,
+ enabled);
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.enablePartition))
+ {
+ jsonParameters.verifyCommand(ClusterSetup.enablePartition);
+
+ boolean enabled =
+ Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
+
+ String[] partitions =
+ jsonParameters.getParameter(JsonParameters.PARTITION).split(";");
+ String resource =
+ jsonParameters.getParameter(JsonParameters.RESOURCE);
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.getClusterManagementTool().enablePartition(enabled,
+ clusterName,
+ instanceName,
+ resource,
+ Arrays.asList(partitions));
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.resetPartition))
+ {
+ jsonParameters.verifyCommand(ClusterSetup.resetPartition);
+
+ String resource =
+ jsonParameters.getParameter(JsonParameters.RESOURCE);
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ String[] partitionNames =
+ jsonParameters.getParameter(JsonParameters.PARTITION).split("\\s+");
+ setupTool.getClusterManagementTool()
+ .resetPartition(clusterName,
+ instanceName,
+ resource,
+ Arrays.asList(partitionNames));
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.resetInstance))
+ {
+ jsonParameters.verifyCommand(ClusterSetup.resetInstance);
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.getClusterManagementTool().resetInstance(clusterName,
+ Arrays.asList(instanceName));
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.enableInstance + ", "
+ + ClusterSetup.enablePartition + ", " + ClusterSetup.resetInstance + "]");
+ }
+
+ getResponse().setEntity(getInstanceRepresentation());
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("", e);
+ }
+ }
+
+ @Override
+ public void removeRepresentations()
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String instanceName = (String) getRequest().getAttributes().get("instanceName");
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.dropInstanceFromCluster(clusterName, instanceName);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("Error in remove", e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstancesResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstancesResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstancesResource.java
new file mode 100644
index 0000000..53537a6
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/InstancesResource.java
@@ -0,0 +1,192 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.util.Map;
+
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixException;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.helix.model.LiveInstance;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class InstancesResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(InstancesResource.class);
+
+ public InstancesResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ presentation = getInstancesRepresentation(clusterName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getInstancesRepresentation(String clusterName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ HelixDataAccessor accessor =
+ ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
+ Map<String, LiveInstance> liveInstancesMap =
+ accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
+ Map<String, InstanceConfig> instanceConfigsMap =
+ accessor.getChildValuesMap(accessor.keyBuilder().instanceConfigs());
+
+ for (String instanceName : instanceConfigsMap.keySet())
+ {
+ boolean isAlive = liveInstancesMap.containsKey(instanceName);
+ instanceConfigsMap.get(instanceName)
+ .getRecord()
+ .setSimpleField("Alive", isAlive + "");
+ }
+
+ StringRepresentation representation =
+ new StringRepresentation(ClusterRepresentationUtil.ObjectToJson(instanceConfigsMap.values()),
+ MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+
+ if (command.equalsIgnoreCase(ClusterSetup.addInstance)
+ || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.addInstance)
+ .contains(command))
+ {
+ if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAME) != null)
+ {
+ setupTool.addInstanceToCluster(clusterName,
+ jsonParameters.getParameter(JsonParameters.INSTANCE_NAME));
+ }
+ else if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES) != null)
+ {
+ setupTool.addInstancesToCluster(clusterName,
+ jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES)
+ .split(";"));
+ }
+ else
+ {
+ throw new HelixException("Missing Json paramaters: '"
+ + JsonParameters.INSTANCE_NAME + "' or '" + JsonParameters.INSTANCE_NAMES
+ + "' ");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.swapInstance))
+ {
+ if (jsonParameters.getParameter(JsonParameters.NEW_INSTANCE) == null
+ || jsonParameters.getParameter(JsonParameters.OLD_INSTANCE) == null)
+ {
+ throw new HelixException("Missing Json paramaters: '"
+ + JsonParameters.NEW_INSTANCE + "' or '" + JsonParameters.OLD_INSTANCE
+ + "' ");
+ }
+ setupTool.swapInstance(clusterName,
+ jsonParameters.getParameter(JsonParameters.OLD_INSTANCE),
+ jsonParameters.getParameter(JsonParameters.NEW_INSTANCE));
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.addInstance + ", "
+ + ClusterSetup.swapInstance + "]");
+ }
+
+ getResponse().setEntity(getInstancesRepresentation(clusterName));
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("", e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/JsonParameters.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/JsonParameters.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/JsonParameters.java
new file mode 100644
index 0000000..fdae9eb
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/JsonParameters.java
@@ -0,0 +1,248 @@
+package org.apache.helix.webapp.resources;
+
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.tools.ClusterSetup;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.restlet.data.Form;
+import org.restlet.resource.Representation;
+
+
+public class JsonParameters
+{
+ // json parameter key
+ public static final String JSON_PARAMETERS = "jsonParameters";
+
+ // json parameter map keys
+ public static final String PARTITION = "partition";
+ public static final String RESOURCE = "resource";
+ public static final String MANAGEMENT_COMMAND = "command";
+ public static final String ENABLED = "enabled";
+ public static final String GRAND_CLUSTER = "grandCluster";
+ public static final String REPLICAS = "replicas";
+ public static final String RESOURCE_KEY_PREFIX = "key";
+ public static final String INSTANCE_NAME = "instanceName";
+ public static final String INSTANCE_NAMES = "instanceNames";
+ public static final String OLD_INSTANCE = "oldInstance";
+ public static final String NEW_INSTANCE = "newInstance";
+ public static final String CONFIGS = "configs";
+ public static final String CLUSTER_NAME = "clusterName";
+ public static final String PARTITIONS = "partitions";
+ public static final String RESOURCE_GROUP_NAME = "resourceGroupName";
+ public static final String STATE_MODEL_DEF_REF = "stateModelDefRef";
+ public static final String IDEAL_STATE_MODE = "mode";
+
+ // zk commands
+ public static final String ZK_DELETE_CHILDREN = "zkDeleteChildren";
+
+ // extra json parameter map keys
+ public static final String NEW_IDEAL_STATE = "newIdealState";
+ public static final String NEW_STATE_MODEL_DEF = "newStateModelDef";
+
+ // aliases for ClusterSetup commands
+ public static Map<String, Set<String>> CLUSTERSETUP_COMMAND_ALIASES;
+ static
+ {
+ CLUSTERSETUP_COMMAND_ALIASES = new HashMap<String, Set<String>>();
+ CLUSTERSETUP_COMMAND_ALIASES.put(ClusterSetup.addResource,
+ new HashSet<String>(Arrays.asList(new String[] { "addResourceGroup" })));
+ CLUSTERSETUP_COMMAND_ALIASES.put(ClusterSetup.activateCluster,
+ new HashSet<String>(Arrays.asList(new String[] { "enableStorageCluster" })));
+ CLUSTERSETUP_COMMAND_ALIASES.put(ClusterSetup.addInstance,
+ new HashSet<String>(Arrays.asList(new String[] { "addInstance" })));
+ }
+
+ // parameter map
+ final Map<String, String> _parameterMap;
+
+ // extra parameters map
+ final Map<String, ZNRecord> _extraParameterMap =
+ new HashMap<String, ZNRecord>();
+
+ public JsonParameters(Representation entity) throws Exception
+ {
+ Form form = new Form(entity);
+
+ // get parameters in String format
+ String jsonPayload = form.getFirstValue(JSON_PARAMETERS, true);
+ if (jsonPayload == null || jsonPayload.isEmpty())
+ {
+ _parameterMap = Collections.emptyMap();
+ }
+ else
+ {
+ _parameterMap = ClusterRepresentationUtil.JsonToMap(jsonPayload);
+ }
+
+ // get extra parameters in ZNRecord format
+ ObjectMapper mapper = new ObjectMapper();
+ String newIdealStateString = form.getFirstValue(NEW_IDEAL_STATE, true);
+
+ if (newIdealStateString != null)
+ {
+ ZNRecord newIdealState =
+ mapper.readValue(new StringReader(newIdealStateString), ZNRecord.class);
+ _extraParameterMap.put(NEW_IDEAL_STATE, newIdealState);
+ }
+
+ String newStateModelString = form.getFirstValue(NEW_STATE_MODEL_DEF, true);
+ if (newStateModelString != null)
+ {
+ ZNRecord newStateModel =
+ mapper.readValue(new StringReader(newStateModelString), ZNRecord.class);
+ _extraParameterMap.put(NEW_STATE_MODEL_DEF, newStateModel);
+ }
+ }
+
+ public String getParameter(String key)
+ {
+ return _parameterMap.get(key);
+ }
+
+ public String getCommand()
+ {
+ String command = _parameterMap.get(MANAGEMENT_COMMAND);
+ return command;
+ }
+
+ public ZNRecord getExtraParameter(String key)
+ {
+ return _extraParameterMap.get(key);
+ }
+
+ public Map<String, String> cloneParameterMap()
+ {
+ Map<String, String> parameterMap = new TreeMap<String, String>();
+ parameterMap.putAll(_parameterMap);
+ return parameterMap;
+ }
+
+ public void verifyCommand(String command)
+ {
+
+ // verify command itself
+ if (_parameterMap.isEmpty())
+ {
+ throw new HelixException("'" + JSON_PARAMETERS + "' in the POST body is empty");
+ }
+
+ if (!_parameterMap.containsKey(MANAGEMENT_COMMAND))
+ {
+ throw new HelixException("Missing management paramater '" + MANAGEMENT_COMMAND
+ + "'");
+ }
+
+ if (!_parameterMap.get(MANAGEMENT_COMMAND).equalsIgnoreCase(command)
+ && !(CLUSTERSETUP_COMMAND_ALIASES.get(command) != null && CLUSTERSETUP_COMMAND_ALIASES.get(command)
+ .contains(_parameterMap.get(MANAGEMENT_COMMAND))))
+ {
+ throw new HelixException(MANAGEMENT_COMMAND + " must be '" + command + "'");
+ }
+
+ // verify command parameters
+ if (command.equalsIgnoreCase(ClusterSetup.enableInstance))
+ {
+ if (!_parameterMap.containsKey(ENABLED))
+ {
+ throw new HelixException("Missing Json parameters: '" + ENABLED + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.enablePartition))
+ {
+ if (!_parameterMap.containsKey(ENABLED))
+ {
+ throw new HelixException("Missing Json parameters: '" + ENABLED + "'");
+ }
+
+ if (!_parameterMap.containsKey(PARTITION))
+ {
+ throw new HelixException("Missing Json parameters: '" + PARTITION + "'");
+ }
+
+ if (!_parameterMap.containsKey(RESOURCE))
+ {
+ throw new HelixException("Missing Json parameters: '" + RESOURCE + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.resetPartition))
+ {
+ if (!_parameterMap.containsKey(PARTITION))
+ {
+ throw new HelixException("Missing Json parameters: '" + PARTITION + "'");
+ }
+
+ if (!_parameterMap.containsKey(RESOURCE))
+ {
+ throw new HelixException("Missing Json parameters: '" + RESOURCE + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.resetInstance))
+ {
+ // nothing
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.activateCluster))
+ {
+ if (!_parameterMap.containsKey(GRAND_CLUSTER))
+ {
+ throw new HelixException("Missing Json parameters: '" + GRAND_CLUSTER + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.setConfig))
+ {
+ if (!_parameterMap.containsKey(CONFIGS))
+ {
+ throw new HelixException("Missing Json parameters: '" + CONFIGS + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.removeConfig))
+ {
+ if (!_parameterMap.containsKey(CONFIGS))
+ {
+ throw new HelixException("Missing Json parameters: '" + CONFIGS + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.addCluster))
+ {
+ if (!_parameterMap.containsKey(CLUSTER_NAME))
+ {
+ throw new HelixException("Missing Json parameters: '" + CLUSTER_NAME + "'");
+ }
+ }
+ else if (command.equalsIgnoreCase(ClusterSetup.addResource))
+ {
+ if (!_parameterMap.containsKey(RESOURCE_GROUP_NAME))
+ {
+ throw new HelixException("Missing Json paramaters: '" + RESOURCE_GROUP_NAME + "'");
+ }
+
+ if (!_parameterMap.containsKey(PARTITIONS))
+ {
+ throw new HelixException("Missing Json paramaters: '" + PARTITIONS + "'");
+ }
+
+ if (!_parameterMap.containsKey(STATE_MODEL_DEF_REF))
+ {
+ throw new HelixException("Missing Json paramaters: '" + STATE_MODEL_DEF_REF + "'");
+ }
+
+ }
+ }
+
+ // temp test
+ public static void main(String[] args) throws Exception
+ {
+ String jsonPayload =
+ "{\"command\":\"resetPartition\",\"resource\": \"DB-1\",\"partition\":\"DB-1_22 DB-1_23\"}";
+ Map<String, String> map = ClusterRepresentationUtil.JsonToMap(jsonPayload);
+ System.out.println(map);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupResource.java
new file mode 100644
index 0000000..3b3e817
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupResource.java
@@ -0,0 +1,173 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ResourceGroupResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ResourceGroupResource.class);
+
+ public ResourceGroupResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return true;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceName = (String) getRequest().getAttributes().get("resourceName");
+ presentation = getIdealStateRepresentation(clusterName, resourceName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getIdealStateRepresentation(String clusterName, String resourceName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ String message =
+ ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
+ clusterName,
+ keyBuilder.idealStates(resourceName),
+ MediaType.APPLICATION_JSON);
+
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void removeRepresentations()
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceGroupName =
+ (String) getRequest().getAttributes().get("resourceName");
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.dropResourceFromCluster(clusterName, resourceGroupName);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("", e);
+ }
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String resourceName = (String) getRequest().getAttributes().get("resourceName");
+
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+ if (command.equalsIgnoreCase(ClusterSetup.resetResource))
+ {
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.getClusterManagementTool().resetResource(clusterName, Arrays.asList(resourceName));
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.resetResource + "]");
+ }
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("", e);
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupsResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupsResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupsResource.java
new file mode 100644
index 0000000..b1a8cb6
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/ResourceGroupsResource.java
@@ -0,0 +1,174 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.util.List;
+
+import org.apache.helix.HelixException;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.IdealState.IdealStateModeProperty;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class ResourceGroupsResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(ResourceGroupsResource.class);
+
+ public ResourceGroupsResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ presentation = getHostedEntitiesRepresentation(clusterName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ;
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ List<String> hostedEntities =
+ setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
+
+ ZNRecord hostedEntitiesRecord = new ZNRecord("ResourceGroups");
+ hostedEntitiesRecord.setListField("ResourceGroups", hostedEntities);
+
+ StringRepresentation representation =
+ new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord),
+ MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+
+ if (command.equalsIgnoreCase(ClusterSetup.addResource)
+ || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.addResource)
+ .contains(command))
+ {
+ jsonParameters.verifyCommand(ClusterSetup.addResource);
+
+ String entityName =
+ jsonParameters.getParameter(JsonParameters.RESOURCE_GROUP_NAME);
+ String stateModelDefRef =
+ jsonParameters.getParameter(JsonParameters.STATE_MODEL_DEF_REF);
+ int partitions =
+ Integer.parseInt(jsonParameters.getParameter(JsonParameters.PARTITIONS));
+ String mode = IdealStateModeProperty.AUTO.toString();
+ if (jsonParameters.getParameter(JsonParameters.IDEAL_STATE_MODE) != null)
+ {
+ mode = jsonParameters.getParameter(JsonParameters.IDEAL_STATE_MODE);
+ }
+
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ;
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ setupTool.addResourceToCluster(clusterName,
+ entityName,
+ partitions,
+ stateModelDefRef,
+ mode);
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.addResource + "]");
+
+ }
+
+ getResponse().setEntity(getHostedEntitiesRepresentation(clusterName));
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("Error in posting " + entity, e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/SchedulerTasksResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/SchedulerTasksResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/SchedulerTasksResource.java
new file mode 100644
index 0000000..b74cfd8
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/SchedulerTasksResource.java
@@ -0,0 +1,186 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixException;
+import org.apache.helix.InstanceType;
+import org.apache.helix.PropertyPathConfig;
+import org.apache.helix.PropertyType;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.LiveInstance;
+import org.apache.helix.model.Message;
+import org.apache.helix.model.Message.MessageType;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.Form;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+/**
+ * This resource can be used to send scheduler tasks to the controller.
+ *
+ * */
+public class SchedulerTasksResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(SchedulerTasksResource.class);
+
+ public static String CRITERIA = "Criteria";
+ public static String MESSAGETEMPLATE = "MessageTemplate";
+ public SchedulerTasksResource(Context context,
+ Request request,
+ Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ presentation = getSchedulerTasksRepresentation();
+ }
+
+ catch(Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getSchedulerTasksRepresentation() throws JsonGenerationException, JsonMappingException, IOException
+ {
+ String clusterName = (String)getRequest().getAttributes().get("clusterName");
+ String instanceName = (String)getRequest().getAttributes().get("instanceName");
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+ ClusterSetup setupTool = new ClusterSetup(zkClient);
+ List<String> instances = setupTool.getClusterManagementTool().getInstancesInCluster(clusterName);
+
+ HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
+ LiveInstance liveInstance = accessor.getProperty(accessor.keyBuilder().liveInstance(instanceName));
+ String sessionId = liveInstance.getSessionId();
+
+ StringRepresentation representation = new StringRepresentation("");//(ClusterRepresentationUtil.ObjectToJson(instanceConfigs), MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String)getRequest().getAttributes().get("clusterName");
+ Form form = new Form(entity);
+ ZkClient zkClient = (ZkClient)getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ String msgTemplateString = ClusterRepresentationUtil.getFormJsonParameterString(form, MESSAGETEMPLATE);
+ if(msgTemplateString == null)
+ {
+ throw new HelixException("SchedulerTasksResource need to have MessageTemplate specified.");
+ }
+ Map<String, String> messageTemplate = ClusterRepresentationUtil.getFormJsonParameters(form, MESSAGETEMPLATE);
+
+ String criteriaString = ClusterRepresentationUtil.getFormJsonParameterString(form, CRITERIA);
+ if(criteriaString == null)
+ {
+ throw new HelixException("SchedulerTasksResource need to have Criteria specified.");
+ }
+
+ Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG, UUID.randomUUID().toString());
+ schedulerMessage.getRecord().getSimpleFields().put(CRITERIA, criteriaString);
+
+ schedulerMessage.getRecord().getMapFields().put(MESSAGETEMPLATE, messageTemplate);
+
+ schedulerMessage.setTgtSessionId("*");
+ schedulerMessage.setTgtName("CONTROLLER");
+ schedulerMessage.setSrcInstanceType(InstanceType.CONTROLLER);
+
+ HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
+ accessor.setProperty(accessor.keyBuilder().controllerMessage(schedulerMessage.getMsgId()), schedulerMessage);
+
+ Map<String, String> resultMap = new HashMap<String, String>();
+ resultMap.put("StatusUpdatePath", PropertyPathConfig.getPath(PropertyType.STATUSUPDATES_CONTROLLER, clusterName, MessageType.SCHEDULER_MSG.toString(),schedulerMessage.getMsgId()));
+ resultMap.put("MessageType", Message.MessageType.SCHEDULER_MSG.toString());
+ resultMap.put("MsgId", schedulerMessage.getMsgId());
+
+ // Assemble the rest URL for task status update
+ String ipAddress = InetAddress.getLocalHost().getCanonicalHostName();
+ String url = "http://" + ipAddress+":" + getContext().getAttributes().get(RestAdminApplication.PORT)
+ + "/clusters/" + clusterName+"/Controller/statusUpdates/SCHEDULER_MSG/" + schedulerMessage.getMsgId();
+ resultMap.put("statusUpdateUrl", url);
+
+ getResponse().setEntity(ClusterRepresentationUtil.ObjectToJson(resultMap), MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch(Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("", e);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/437eb42e/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/StateModelResource.java
----------------------------------------------------------------------
diff --git a/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/StateModelResource.java b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/StateModelResource.java
new file mode 100644
index 0000000..44a05c7
--- /dev/null
+++ b/helix-admin-webapp/src/main/java/org/apache/helix/webapp/resources/StateModelResource.java
@@ -0,0 +1,157 @@
+/**
+ * Copyright (C) 2012 LinkedIn Inc <op...@linkedin.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.helix.webapp.resources;
+
+import java.io.IOException;
+
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.HelixException;
+import org.apache.helix.PropertyKey;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.StateModelDefinition;
+import org.apache.helix.tools.ClusterSetup;
+import org.apache.helix.webapp.RestAdminApplication;
+import org.apache.log4j.Logger;
+import org.codehaus.jackson.JsonGenerationException;
+import org.codehaus.jackson.map.JsonMappingException;
+import org.restlet.Context;
+import org.restlet.data.MediaType;
+import org.restlet.data.Request;
+import org.restlet.data.Response;
+import org.restlet.data.Status;
+import org.restlet.resource.Representation;
+import org.restlet.resource.Resource;
+import org.restlet.resource.StringRepresentation;
+import org.restlet.resource.Variant;
+
+
+public class StateModelResource extends Resource
+{
+ private final static Logger LOG = Logger.getLogger(StateModelResource.class);
+
+ public StateModelResource(Context context, Request request, Response response)
+ {
+ super(context, request, response);
+ getVariants().add(new Variant(MediaType.TEXT_PLAIN));
+ getVariants().add(new Variant(MediaType.APPLICATION_JSON));
+ }
+
+ @Override
+ public boolean allowGet()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPost()
+ {
+ return true;
+ }
+
+ @Override
+ public boolean allowPut()
+ {
+ return false;
+ }
+
+ @Override
+ public boolean allowDelete()
+ {
+ return false;
+ }
+
+ @Override
+ public Representation represent(Variant variant)
+ {
+ StringRepresentation presentation = null;
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String modelName = (String) getRequest().getAttributes().get("modelName");
+ presentation = getStateModelRepresentation(clusterName, modelName);
+ }
+
+ catch (Exception e)
+ {
+ String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
+ presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
+
+ LOG.error("", e);
+ }
+ return presentation;
+ }
+
+ StringRepresentation getStateModelRepresentation(String clusterName, String modelName) throws JsonGenerationException,
+ JsonMappingException,
+ IOException
+ {
+ Builder keyBuilder = new PropertyKey.Builder(clusterName);
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ String message =
+ ClusterRepresentationUtil.getClusterPropertyAsString(zkClient,
+ clusterName,
+ keyBuilder.stateModelDef(modelName),
+ MediaType.APPLICATION_JSON);
+
+ StringRepresentation representation =
+ new StringRepresentation(message, MediaType.APPLICATION_JSON);
+
+ return representation;
+ }
+
+ @Override
+ public void acceptRepresentation(Representation entity)
+ {
+ try
+ {
+ String clusterName = (String) getRequest().getAttributes().get("clusterName");
+ String modelName = (String) getRequest().getAttributes().get("modelName");
+ ZkClient zkClient =
+ (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
+
+ JsonParameters jsonParameters = new JsonParameters(entity);
+ String command = jsonParameters.getCommand();
+
+ if (command.equalsIgnoreCase(ClusterSetup.addStateModelDef))
+ {
+ ZNRecord newStateModel = jsonParameters.getExtraParameter(JsonParameters.NEW_STATE_MODEL_DEF);
+ HelixDataAccessor accessor =
+ ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
+
+ accessor.setProperty(accessor.keyBuilder().stateModelDef(newStateModel.getId()),
+ new StateModelDefinition(newStateModel));
+ }
+ else
+ {
+ throw new HelixException("Unsupported command: " + command
+ + ". Should be one of [" + ClusterSetup.addStateModelDef + "]");
+ }
+ getResponse().setEntity(getStateModelRepresentation(clusterName, modelName));
+ getResponse().setStatus(Status.SUCCESS_OK);
+ }
+ catch (Exception e)
+ {
+ getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e),
+ MediaType.APPLICATION_JSON);
+ getResponse().setStatus(Status.SUCCESS_OK);
+ LOG.error("Error in posting " + entity, e);
+ }
+ }
+}