summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/tasks/MigrateBrickTask.java
blob: 7e61f615040d98f7024caebd06508ea60f12f9a0 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*******************************************************************************
 * 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 MigrateBrickTask extends Task {

	private String fromBrick;
	private String toBrick;
	private Boolean autoCommit;
	private GlusterInterfaceService glusterInterface;
	protected ServerUtil serverUtil;

	public String getFromBrick() {
		return fromBrick;
	}

	public void setFromBrick(String fromBrick) {
		this.fromBrick = fromBrick;
	}

	public String getToBrick() {
		return toBrick;
	}

	public void setToBrick(String toBrick) {
		this.toBrick = toBrick;
	}

	public Boolean getAutoCommit() {
		return autoCommit;
	}

	public void setAutoCommit(Boolean autoCommit) {
		this.autoCommit = autoCommit;
	}

	public MigrateBrickTask(ClusterService clusterService, String clusterName, String volumeName, String fromBrick,
			String toBrick) {
		super(clusterService, clusterName, TASK_TYPE.BRICK_MIGRATE, volumeName + "#" + fromBrick + "#" + toBrick,
				"Brick Migration on volume [" + volumeName + "] from [" + fromBrick + "] to [" + toBrick + "]", true,
				true, true);
		setFromBrick(fromBrick);
		setToBrick(toBrick);
		taskInfo.setName(getId());
		init();
	}
	
	private void init() {
		ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
		glusterInterface = ctx.getBean(GlusterInterfaceService.class);
		serverUtil = ctx.getBean(ServerUtil.class);
	}

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

	@Override
	public void start() {
		try {
			startMigration(getOnlineServer().getName());
		} 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.
				startMigration(getNewOnlineServer().getName());
			} else {
				throw new GlusterRuntimeException("Error while starting migration!", e);
			}
		}
	}

	private void startMigration(String onlineServerName) {
		String volumeName = getTaskInfo().getReference().split("#")[0];
		glusterInterface.startBrickMigration(onlineServerName, volumeName, getFromBrick(), getToBrick());
		getTaskInfo().setStatus(new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, "Brick Migration Started.")));
		System.out.println(getTaskInfo().getStatus().toString());
	}

	@Override
	public void pause() {
		try {
			pauseMigration(getOnlineServer().getName());
		} 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.
				pauseMigration(getNewOnlineServer().getName());
			} else {
				throw new GlusterRuntimeException(e.getMessage());
			}
		}
	}

	private void pauseMigration(String onlineServer) {
		String volumeName = getTaskInfo().getReference().split("#")[0];
		glusterInterface.pauseBrickMigration(onlineServer, volumeName, getFromBrick(), getToBrick());
		TaskStatus taskStatus = new TaskStatus();
		taskStatus.setCode(Status.STATUS_CODE_PAUSE);
		taskStatus.setMessage("Brick Migration Paused");
		getTaskInfo().setStatus(taskStatus);
	}

	@Override
	public void resume() {
		start();
	}

	@Override
	public void commit() {
		try {
			commitMigration(getOnlineServer().getName());
		} 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.
				commitMigration(getNewOnlineServer().getName());
			} else {
				throw new GlusterRuntimeException(e.getMessage());
			}
		}
	}
	
	private void commitMigration(String serverName) {
		String volumeName = getTaskInfo().getReference().split("#")[0];
		glusterInterface.commitBrickMigration(serverName, volumeName, getFromBrick(), getToBrick());
		TaskStatus taskStatus = new TaskStatus();
		taskStatus.setCode(Status.STATUS_CODE_SUCCESS);
		taskStatus.setMessage("Brick Migration Committed.");
		getTaskInfo().setStatus(taskStatus);
	}

	@Override
	public void stop() {
		try {
			stopMigration(getOnlineServer().getName());
		} 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.
				stopMigration(getNewOnlineServer().getName());
			} else {
				throw new GlusterRuntimeException(e.getMessage());
			}
		}
	}

	private void stopMigration(String serverName) {
		String volumeName = getTaskInfo().getReference().split("#")[0];
		glusterInterface.stopBrickMigration(serverName, volumeName, getFromBrick(), getToBrick());
		TaskStatus taskStatus = new TaskStatus();
		taskStatus.setCode(Status.STATUS_CODE_SUCCESS);
		taskStatus.setMessage("Brick Migration Stopped");
		getTaskInfo().setStatus(taskStatus);
	}

	@Override
	public TaskStatus checkStatus() {
		try {
			return checkMigrationStatus(getOnlineServer().getName());
		} 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.
				return checkMigrationStatus(getNewOnlineServer().getName());
			} else {
				if(e instanceof GlusterRuntimeException) {
					throw ((GlusterRuntimeException)e);
				} else {
					throw new GlusterRuntimeException("Exception while checking brick migration status!", e);
				}
			}
		}
	}
	
	private TaskStatus checkMigrationStatus(String serverName) {
		// For committed task, status command (CLI) is invalid, just return current status
		if (taskInfo.getStatus().getCode() == Status.STATUS_CODE_SUCCESS) {
			return taskInfo.getStatus();
		}

		String volumeName = getTaskInfo().getReference().split("#")[0];
		TaskStatus taskStatus = glusterInterface.checkBrickMigrationStatus(serverName, volumeName, getFromBrick(),
				getToBrick());
		if (autoCommit && taskStatus.isCommitPending()) {
			commitMigration(serverName);
			return taskInfo.getStatus(); // return the committed status
		}

		taskInfo.setStatus(taskStatus); // Update the task status
		return taskStatus;
	}
}