summaryrefslogtreecommitdiffstats
path: root/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/tasks/MigrateBrickTask.java
blob: 5d321d71b04a89bf1d6c57ba8e3045e8d11a32ec (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**
 * MigrateDiskTask.java
 *
 * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Console.
 *
 * Gluster Management Console is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero 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 Console 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 Affero General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package com.gluster.storage.management.gateway.tasks;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoader;

import com.gluster.storage.management.core.exceptions.ConnectionException;
import com.gluster.storage.management.core.exceptions.GlusterRuntimeException;
import com.gluster.storage.management.core.model.Status;
import com.gluster.storage.management.core.model.TaskInfo.TASK_TYPE;
import com.gluster.storage.management.core.model.TaskStatus;
import com.gluster.storage.management.core.utils.ProcessResult;
import com.gluster.storage.management.gateway.services.ClusterService;
import com.gluster.storage.management.gateway.utils.GlusterUtil;
import com.gluster.storage.management.gateway.utils.ServerUtil;
import com.sun.jersey.core.util.Base64;

public class MigrateBrickTask extends Task {

	private String fromBrick;
	private String toBrick;
	private Boolean autoCommit;
	private GlusterUtil glusterUtil;
	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();
		glusterUtil = ctx.getBean(GlusterUtil.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(e.getMessage());
			}
		}
	}

	private void startMigration(String onlineServerName) {
		String volumeName = getTaskInfo().getReference().split("#")[0];
		String output = glusterUtil.executeBrickMigration(onlineServerName, volumeName,
				getFromBrick(), getToBrick(), "start");
		if (output.matches(".*started successfully$")) {
			getTaskInfo().setStatus(
					new TaskStatus(new Status(Status.STATUS_CODE_RUNNING, output)));
		}
	}

	@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];
		String output = glusterUtil.executeBrickMigration(onlineServer, volumeName,
				getFromBrick(), getToBrick(), "pause");
		TaskStatus taskStatus = new TaskStatus();
		if (output.matches(".*paused successfully$")) { 
			taskStatus.setCode(Status.STATUS_CODE_PAUSE);
			taskStatus.setMessage(output);
			getTaskInfo().setStatus(taskStatus);
			return;
		}
	}

	@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];
		String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(), getToBrick(),
				"commit");
		TaskStatus taskStatus = new TaskStatus();
		if (output.matches(".*commit successful$")) {
			taskStatus.setCode(Status.STATUS_CODE_SUCCESS);
			taskStatus.setMessage(output);
			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];
		String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(),
				getToBrick(), "abort");
		TaskStatus taskStatus = new TaskStatus();
		if (output.matches(".*aborted successfully$")) {
			taskStatus.setCode(Status.STATUS_CODE_SUCCESS);
			taskStatus.setMessage(output);
			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());
			} 
		}
		return null;
	}
	
	private TaskStatus checkMigrationStatus(String serverName) {
		// For committed task, status command (CLI) is invalid, just return current status
		if (getTaskInfo().getStatus().getCode() == Status.STATUS_CODE_SUCCESS) {
			return getTaskInfo().getStatus();
		}

		TaskStatus taskStatus = new TaskStatus();
		String volumeName = getTaskInfo().getReference().split("#")[0];
		String output = glusterUtil.executeBrickMigration(serverName, volumeName, getFromBrick(),
				getToBrick(), "status");

		if (output.matches("^Number of files migrated.*Migration complete$")
				|| output.matches("^Number of files migrated = 0 .*Current file=")) {
			// Note: Workaround - if no file in the volume brick to migrate,
			// Gluster CLI is not giving proper (complete) status
			taskStatus.setCode(Status.STATUS_CODE_COMMIT_PENDING);
			if (autoCommit) {
				commitMigration(serverName);
				return getTaskInfo().getStatus(); // return the committed status
			} else {
				taskStatus.setMessage(output.replaceAll("Migration complete", "Commit pending"));
			}
		} else if (output.matches("^Number of files migrated.*Current file=.*")) {
			taskStatus.setCode(Status.STATUS_CODE_RUNNING);
		} else if (output.matches("^replace brick has been paused.*")) {
			taskStatus.setCode(Status.STATUS_CODE_PAUSE);
		} else {
			taskStatus.setCode(Status.STATUS_CODE_FAILURE);
		}
		taskStatus.setMessage(output);
		taskInfo.setStatus(taskStatus); // Update the task status
		return taskStatus;
	}
}