Permalink
Browse files

Update database on Add/Remove gluster volume brick

  • Loading branch information...
1 parent 5d22a4d commit 7e624154dab224d7ab5939ef4f5b10a24a4769d3 @Dhandapani committed Mar 13, 2012
@@ -18,6 +18,17 @@ INSERT INTO gluster_volume_bricks(volume_id, host_id, brick_dir, status)
END; $procedure$
LANGUAGE plpgsql;
+Create or replace FUNCTION RemoveGlusterVolumeBrick(v_volume_id UUID, v_host_id UUID, v_brick_dir VARCHAR(4096))
@anjalshireesh

anjalshireesh Mar 13, 2012

A function (DeleteGlusterVolumeBrick) already exists for exactly same purpose.

+RETURNS VOID
+ AS $procedure$
+BEGIN
+ DELETE FROM gluster_volume_bricks
+ WHERE volume_id = v_volume_id
+ AND host_id = v_host_id
+ AND brick_dir = v_brick_dir;
+END; $procedure$
+LANGUAGE plpgsql;
+
Create or replace FUNCTION InsertGlusterVolumeOption(v_volume_id UUID, v_option_key VARCHAR(8192), v_option_val VARCHAR(8192))
RETURNS VOID
@@ -1,12 +1,21 @@
package org.ovirt.engine.core.bll.gluster;
+import java.util.List;
+
import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.common.AuditLogType;
+import org.ovirt.engine.core.common.businessentities.GlusterBrickEntity;
+import org.ovirt.engine.core.common.businessentities.GlusterVolumeEntity;
+import org.ovirt.engine.core.common.errors.VdcBLLException;
import org.ovirt.engine.core.common.glusteractions.GlusterVolumeBricksParameters;
import org.ovirt.engine.core.common.glustercommands.GlusterVolumeBricksVDSParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.VdcBllMessages;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.utils.transaction.TransactionMethod;
+import org.ovirt.engine.core.utils.transaction.TransactionSupport;
public class AddBricksToGlusterVolumeCommand extends GlusterCommandBase<GlusterVolumeBricksParameters> {
@@ -23,18 +32,42 @@ protected boolean canDoAction() {
@Override
protected void executeCommand() {
- VDSReturnValue returnValue =
- Backend
- .getInstance()
- .getResourceManager()
- .RunVdsCommand(
- VDSCommandType.AddBricksToGlusterVolume,
- new GlusterVolumeBricksVDSParameters(getOnlineHost().getvds_id(),
+ try {
+ TransactionSupport.executeInNewTransaction(new TransactionMethod<Void>() {
+
+ @Override
+ public Void runInTransaction() {
+ updateGlusterVolumeBricksInDb(getVdsGroupId(),
@anjalshireesh

anjalshireesh Mar 13, 2012

why update instead of add ?

getParameters().getVolumeName(),
- getParameters().getBricks()));
- setSucceeded(returnValue.getSucceeded());
+ getParameters().getBricks());
+ VDSReturnValue returnValue =
+ Backend
+ .getInstance()
+ .getResourceManager()
+ .RunVdsCommand(
+ VDSCommandType.AddBricksToGlusterVolume,
+ new GlusterVolumeBricksVDSParameters(getOnlineHost().getvds_id(),
+ getParameters().getVolumeName(),
+ getParameters().getBricks()));
+ setSucceeded(returnValue.getSucceeded());
+ return null;
+ }
+ });
+ } catch (VdcBLLException e) {
+ getReturnValue().getExecuteFailedMessages().add(e.getErrorCode().toString());
+ setSucceeded(false);
+ }
}
+ private void updateGlusterVolumeBricksInDb(Guid vdsGroupId, String volumeName, List<GlusterBrickEntity> bricks) {
+ GlusterVolumeEntity volume = DbFacade.getInstance().getGlusterVolumeDAO().getByName(vdsGroupId, volumeName);
+ updateHostIdsInBricks(bricks);
+ for (GlusterBrickEntity brick : bricks) {
@anjalshireesh

anjalshireesh Mar 13, 2012

Will be better if we introduce a method addBricksToVolume in the DAO which calls the single insert method in a loop.

+ DbFacade.getInstance().getGlusterVolumeDAO().addBrickToVolume(volume, brick);
+ }
+ }
+
+
@Override
public AuditLogType getAuditLogTypeValue() {
if(getSucceeded()) {
@@ -3,24 +3,17 @@
*/
package org.ovirt.engine.core.bll.gluster;
-import java.util.List;
-
import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.common.AuditLogType;
-import org.ovirt.engine.core.common.businessentities.GlusterBrickEntity;
import org.ovirt.engine.core.common.businessentities.GlusterVolumeEntity;
import org.ovirt.engine.core.common.businessentities.GlusterVolumeEntity.ACCESS_PROTOCOL;
-import org.ovirt.engine.core.common.businessentities.VDS;
-import org.ovirt.engine.core.common.errors.VdcBLLException;
-import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.common.glusteractions.CreateGlusterVolumeParameters;
import org.ovirt.engine.core.common.glustercommands.CreateGlusterVolumeVDSParameters;
import org.ovirt.engine.core.common.interfaces.VDSBrokerFrontend;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
-import org.ovirt.engine.core.dao.VdsDAO;
import org.ovirt.engine.core.utils.transaction.TransactionMethod;
import org.ovirt.engine.core.utils.transaction.TransactionSupport;
@@ -86,28 +79,11 @@ private void addVolumeToDb(GlusterVolumeEntity createdVolume) {
createdVolume.setClusterId(getVdsGroupId());
// volume fetched from VDSM doesn't contain host id since GlusterFS is not aware of the host id concept
- updateHostIdsInBricks(createdVolume);
+ updateHostIdsInBricks(createdVolume.getBricks());
DbFacade.getInstance().getGlusterVolumeDAO().save(createdVolume);
}
- private void updateHostIdsInBricks(GlusterVolumeEntity createdVolume) {
- VdsDAO hostDAO = DbFacade.getInstance().getVdsDAO();
- for (GlusterBrickEntity brick : createdVolume.getBricks()) {
- // TODO: UI must send server name without spaces
- String serverName = brick.getServerName().trim();
-
- // TODO: Should probably introduce a new method to get host with given name/ip from given cluster id
- List<VDS> hosts = hostDAO.getAllForHostname(serverName);
- if (hosts == null || hosts.isEmpty()) {
- hosts = hostDAO.getAllWithIpAddress(serverName);
- if (hosts == null || hosts.isEmpty()) {
- throw new VdcBLLException(VdcBllErrors.GLUSTER_BRICK_HOST_NOT_FOUND);
- }
- }
- brick.setServerId(hosts.get(0).getvds_id());
- }
- }
@Override
public AuditLogType getAuditLogTypeValue() {
@@ -7,10 +7,14 @@
import org.ovirt.engine.core.bll.VdsGroupCommandBase;
import org.ovirt.engine.core.common.VdcObjectType;
import org.ovirt.engine.core.common.action.VdsGroupParametersBase;
+import org.ovirt.engine.core.common.businessentities.GlusterBrickEntity;
import org.ovirt.engine.core.common.businessentities.VDS;
import org.ovirt.engine.core.common.businessentities.VDSStatus;
+import org.ovirt.engine.core.common.errors.VdcBLLException;
+import org.ovirt.engine.core.common.errors.VdcBllErrors;
import org.ovirt.engine.core.dal.VdcBllMessages;
import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.VdsDAO;
public abstract class GlusterCommandBase<T extends VdsGroupParametersBase> extends VdsGroupCommandBase<T> {
public GlusterCommandBase(T params) {
@@ -50,4 +54,22 @@ public VDS getOnlineHost() {
.getAllForVdsGroupWithStatus(getVdsGroup().getID().getValue(), VDSStatus.Up);
return hosts.get(0);
}
+
+ protected void updateHostIdsInBricks(List<GlusterBrickEntity> bricks) {
@anjalshireesh

anjalshireesh Mar 13, 2012

Perform this operation (updating host ids in bricks) in the Gluster Volume DAO itself.

+ VdsDAO hostDAO = DbFacade.getInstance().getVdsDAO();
+ for (GlusterBrickEntity brick : bricks) {
+ // TODO: UI must send server name without spaces
+ String serverName = brick.getServerName().trim();
+
+ // TODO: Should probably introduce a new method to get host with given name/ip from given cluster id
+ List<VDS> hosts = hostDAO.getAllForHostname(serverName);
+ if (hosts == null || hosts.isEmpty()) {
+ hosts = hostDAO.getAllWithIpAddress(serverName);
+ if (hosts == null || hosts.isEmpty()) {
+ throw new VdcBLLException(VdcBllErrors.GLUSTER_BRICK_HOST_NOT_FOUND);
+ }
+ }
+ brick.setServerId(hosts.get(0).getvds_id());
+ }
+ }
}
@@ -1,12 +1,21 @@
package org.ovirt.engine.core.bll.gluster;
+import java.util.List;
+
import org.ovirt.engine.core.bll.Backend;
import org.ovirt.engine.core.common.AuditLogType;
+import org.ovirt.engine.core.common.businessentities.GlusterBrickEntity;
+import org.ovirt.engine.core.common.businessentities.GlusterVolumeEntity;
+import org.ovirt.engine.core.common.errors.VdcBLLException;
import org.ovirt.engine.core.common.glusteractions.GlusterVolumeBricksParameters;
import org.ovirt.engine.core.common.glustercommands.GlusterVolumeBricksVDSParameters;
import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.compat.Guid;
import org.ovirt.engine.core.dal.VdcBllMessages;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.utils.transaction.TransactionMethod;
+import org.ovirt.engine.core.utils.transaction.TransactionSupport;
public class RemoveBricksFromGlusterVolumeCommand extends GlusterCommandBase<GlusterVolumeBricksParameters> {
@@ -23,14 +32,37 @@ protected boolean canDoAction() {
@Override
protected void executeCommand() {
- VDSReturnValue returnValue =
- Backend.getInstance()
- .getResourceManager()
- .RunVdsCommand(VDSCommandType.RemoveBricksFromGlusterVolume,
- new GlusterVolumeBricksVDSParameters(getOnlineHost().getvds_id(),
+ try {
+ TransactionSupport.executeInNewTransaction(new TransactionMethod<Void>() {
+
+ @Override
+ public Void runInTransaction() {
+ updateGlusterVolumeBricksInDb(getVdsGroupId(),
getParameters().getVolumeName(),
- getParameters().getBricks()));
- setSucceeded(returnValue.getSucceeded());
+ getParameters().getBricks());
+ VDSReturnValue returnValue =
+ Backend.getInstance()
+ .getResourceManager()
+ .RunVdsCommand(VDSCommandType.RemoveBricksFromGlusterVolume,
+ new GlusterVolumeBricksVDSParameters(getOnlineHost().getvds_id(),
+ getParameters().getVolumeName(),
+ getParameters().getBricks()));
+ setSucceeded(returnValue.getSucceeded());
+ return null;
+ }
+ });
+ } catch (VdcBLLException e) {
+ getReturnValue().getExecuteFailedMessages().add(e.getErrorCode().toString());
+ setSucceeded(false);
+ }
+ }
+
+ private void updateGlusterVolumeBricksInDb(Guid vdsGroupId, String volumeName, List<GlusterBrickEntity> bricks) {
+ GlusterVolumeEntity volume = DbFacade.getInstance().getGlusterVolumeDAO().getByName(vdsGroupId, volumeName);
+ updateHostIdsInBricks(bricks);
+ for (GlusterBrickEntity brick : bricks) {
+ DbFacade.getInstance().getGlusterVolumeDAO().removeBrickFromVolume(volume.getId(), brick);
+ }
}
@Override
@@ -28,7 +28,7 @@
public void getVolumesByClusterGuid(Guid clusterId);
- public void addBrickToVolume(Guid volumeId, GlusterBrickEntity brick);
+ public void addBrickToVolume(GlusterVolumeEntity volume, GlusterBrickEntity brick);
@anjalshireesh

anjalshireesh Mar 13, 2012

This change not required.

public void removeBrickFromVolume(Guid volumeId, GlusterBrickEntity brick);
@@ -50,16 +50,21 @@ private void insertVolumeEntity(GlusterVolumeEntity volume) {
private void insertVolumeBricks(GlusterVolumeEntity volume) {
List<GlusterBrickEntity> bricks = volume.getBricks();
for (GlusterBrickEntity brick : bricks) {
- getCallsHandler().executeModification(
- "InsertGlusterVolumeBrick",
- getCustomMapSqlParameterSource()
- .addValue("volume_id", volume.getId())
- .addValue("host_id", brick.getServerId())
- .addValue("brick_dir", brick.getBrickDirectory())
- .addValue("status", volume.getStatus()));
+ insertVolumeBricks(volume, brick);
}
}
+ private void insertVolumeBricks(GlusterVolumeEntity volume, GlusterBrickEntity brick) {
+ getCallsHandler().executeModification(
+ "InsertGlusterVolumeBrick",
+ getCustomMapSqlParameterSource()
+ .addValue("volume_id", volume.getId())
+ .addValue("host_id", brick.getServerId())
+ .addValue("brick_dir", brick.getBrickDirectory())
+ .addValue("status", volume.getStatus()));
@anjalshireesh

anjalshireesh Mar 13, 2012

The status should be passed from the brick object

+ }
+
+
private void insertVolumeOptions(GlusterVolumeEntity volume) {
Collection<GlusterVolumeOption> options = volume.getOptions();
for (GlusterVolumeOption option : options) {
@@ -229,14 +234,18 @@ public void getVolumesByClusterGuid(Guid clusterId) {
}
@Override
- public void addBrickToVolume(Guid volumeId, GlusterBrickEntity brick) {
- // TODO Auto-generated method stub
-
+ public void addBrickToVolume(GlusterVolumeEntity volume, GlusterBrickEntity brick) {
+ insertVolumeBricks(volume, brick);
@anjalshireesh

anjalshireesh Mar 13, 2012

No need of wrapper method. The insert code can directly go here.

}
@Override
public void removeBrickFromVolume(Guid volumeId, GlusterBrickEntity brick) {
- // TODO Auto-generated method stub
+ getCallsHandler().executeModification(
+ "RemoveGlusterVolumeBrick",
+ getCustomMapSqlParameterSource()
+ .addValue("volume_id", volumeId)
+ .addValue("host_id", brick.getServerId())
+ .addValue("brick_dir", brick.getBrickDirectory()));
}
@@ -74,6 +74,38 @@ public void testGlusterVolumeOption2() throws Exception {
assertEquals(newVolumeEntity, volumeEntity);
}
+
+ @Test
+ public void testAddBricksToGlusterVolume() throws Exception {
+ GlusterBrickEntity brick = getGlusterVolumeBrick("/export/testVol2");
+ dao.addBrickToVolume(volume, brick);
+
+ volume.addBrick(brick);
+
+ GlusterVolumeEntity volumeEntity = dao.getById(volume.getId());
+ assertNotNull(volumeEntity);
+ assertEquals(volumeEntity, volume);
+ }
+
+ @Test
+ public void testRemoveBricksFromGlusterVolume() throws Exception {
+ GlusterBrickEntity brick = getGlusterVolumeBrick("/export/testVol2");
+ dao.addBrickToVolume(volume, brick);
+
+ dao.removeBrickFromVolume(volume.getId(), brick);
+
+ GlusterVolumeEntity volumeEntity = dao.getById(volume.getId());
+ assertNotNull(volumeEntity);
+ assertEquals(volumeEntity, volume);
+ }
+
+ private GlusterBrickEntity getGlusterVolumeBrick(String brickDirectory) {
+ GlusterBrickEntity brick = new GlusterBrickEntity(host, brickDirectory);
+ brick.setServerId(host.getId());
+ brick.setStatus(BRICK_STATUS.ONLINE);
+ return brick;
+ }
+
private GlusterVolumeEntity insertTestVolume(boolean setOption) {
Guid volumeId = Guid.NewGuid();
@@ -92,9 +124,7 @@ private GlusterVolumeEntity insertTestVolume(boolean setOption) {
}
volume.setAccessProtocols("GLUSTER,NFS");
- GlusterBrickEntity brick = new GlusterBrickEntity(host, "/export/testVol1");
- brick.setServerId(host.getId());
- brick.setStatus(BRICK_STATUS.ONLINE);
+ GlusterBrickEntity brick = getGlusterVolumeBrick("/export/testVol1");
volume.addBrick(brick);
dao.save(volume);

0 comments on commit 7e62415

Please sign in to comment.