summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/tasks/RebalanceVolumeTask.java
blob: 3f68206b02124587aff5dbeee4f4b50ec10bedf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Gateway.
 *
 * Gluster Management Gateway is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Gateway is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.gateway.tasks;

import org.gluster.storage.management.core.exceptions.ConnectionException;
import org.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import org.gluster.storage.management.core.model.Status;
import org.gluster.storage.management.core.model.TaskStatus;
import org.gluster.storage.management.core.model.TaskInfo.TASK_TYPE;
import org.gluster.storage.management.gateway.services.ClusterService;
import org.gluster.storage.management.gateway.services.GlusterInterfaceService;
import org.gluster.storage.management.gateway.utils.ServerUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.sun.jersey.core.util.Base64;

public class RebalanceVolumeTask extends Task {

	private String layout;
	private String serverName;
	private ServerUtil serverUtil;
	private GlusterInterfaceService glusterUtil;

	public RebalanceVolumeTask(ClusterService clusterService, String clusterName, String volumeName, String layout) {
		super(clusterService, clusterName, TASK_TYPE.VOLUME_REBALANCE, volumeName, "Volume " + volumeName
				+ " Rebalance", false, true, false);
		setLayout(layout);
		taskInfo.setName(getId());
		init();
	}
	
	private void init() {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		serverUtil = ctx.getBean(ServerUtil.class);
		glusterUtil = ctx.getBean(GlusterInterfaceService.class);
	}

	@Override
	public String getId() {
		return new String(Base64.encode(getClusterName() + "-" + taskInfo.getType() + "-" + taskInfo.getReference()));
	}

	@Override
	public void start() {
		try {
			serverName = getOnlineServer().getName();
			startRebalance(serverName);
		} catch (Exception e) {
			// check if online server has gone offline. If yes, try again one more time.
			if (e instanceof ConnectionException || serverUtil.isServerOnline(getOnlineServer()) == false) {
				// online server might have gone offline. try with a new one
				serverName = getNewOnlineServer().getName();
				startRebalance(serverName);
			} else {
				throw new GlusterRuntimeException(e.getMessage());
			}
		}
	}

	private void startRebalance(String serverName) {
		String command = "gluster volume rebalance " + getTaskInfo().getReference() + " " + getLayout() + " start";
		String output = serverUtil.executeOnServer(serverName, command);
		getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output)));
	}

	@Override
	public void resume() {
		getTaskInfo().setStatus(
				new TaskStatus(new Status(Status.STATUS_CODE_FAILURE,
						"Pause/Resume is not supported in Volume Rebalance")));
	}

	@Override
	public void stop() {
		try {
			glusterUtil.stopRebalance(serverName, getTaskInfo().getReference());
		} catch (Exception e) {
			// check if online server has gone offline. If yes, try again one more time.
			if (e instanceof ConnectionException || serverUtil.isServerOnline(getOnlineServer()) == false) {
				// online server might have gone offline. update the failure status
				getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_FAILURE, e.getMessage())));
			} else {
				throw new GlusterRuntimeException(e.getMessage());
			}
		}
	}

	@Override
	public void pause() {
		getTaskInfo().setStatus(
				new TaskStatus(new Status(Status.STATUS_CODE_FAILURE,
						"Pause/Resume is not supported in Volume Rebalance")));
	}

	@Override
	public TaskStatus checkStatus() {
		try {
			return glusterUtil.checkRebalanceStatus(serverName, getTaskInfo().getReference());
		} catch (Exception e) {
			// check if online server has gone offline. If yes, try again one more time.
			if (e instanceof ConnectionException || serverUtil.isServerOnline(getOnlineServer()) == false) {
				// online server might have gone offline. update the failure status
				getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_FAILURE, e.getMessage())));
				return getTaskInfo().getStatus();
			} else {
				getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_ERROR, e.getMessage())));
				return getTaskInfo().getStatus();
			}
		}
	}
	
	public void setLayout(String layout) {
		this.layout = layout;
	}

	public String getLayout() {
		return layout;
	}

	@Override
	public void commit() {
		// TODO Auto-generated method stub
	}
}