Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotService;
import org.apache.cloudstack.engine.subsystem.api.storage.StorageCacheManager;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCallFuture;
import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher;
Expand Down Expand Up @@ -79,6 +80,8 @@ public class SnapshotServiceImpl implements SnapshotService {
StorageCacheManager _cacheMgr;
@Inject
private SnapshotDetailsDao _snapshotDetailsDao;
@Inject
VolumeDataFactory volFactory;

static private class CreateSnapshotContext<T> extends AsyncRpcContext<T> {
final SnapshotInfo snapshot;
Expand Down Expand Up @@ -428,11 +431,15 @@ public boolean deleteSnapshot(SnapshotInfo snapInfo) {

@Override
public boolean revertSnapshot(SnapshotInfo snapshot) {
PrimaryDataStore store = null;
SnapshotInfo snapshotOnPrimaryStore = _snapshotFactory.getSnapshot(snapshot.getId(), DataStoreRole.Primary);
if (snapshotOnPrimaryStore == null) {
throw new CloudRuntimeException("Cannot find an entry for snapshot " + snapshot.getId() + " on primary storage pools");
s_logger.warn("Cannot find an entry for snapshot " + snapshot.getId() + " on primary storage pools, searching with volume's primary storage pool");
VolumeInfo volumeInfo = volFactory.getVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
store = (PrimaryDataStore)volumeInfo.getDataStore();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@harikrishna-patnala should we check for subsequent cases where it's not found again or this is null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are not looking for snapshotOnPrimaryStore again. We are just trying to get the appropriate datastore driver to run the command. We are using volume's info here if snapshot on primary os not found. So need to check again.

} else {
store = (PrimaryDataStore)snapshotOnPrimaryStore.getDataStore();
}
PrimaryDataStore store = (PrimaryDataStore)snapshotOnPrimaryStore.getDataStore();

AsyncCallFuture<SnapshotResult> future = new AsyncCallFuture<SnapshotResult>();
RevertSnapshotContext<CommandResult> context = new RevertSnapshotContext<CommandResult>(null, snapshot, future);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.cloudstack.storage.snapshot;

import com.cloud.storage.DataStoreRole;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStoreDriver;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.SnapshotResult;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCallFuture;
import org.apache.cloudstack.framework.async.AsyncCallbackDispatcher;
import org.apache.cloudstack.storage.command.CommandResult;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

@RunWith(PowerMockRunner.class)
@PrepareForTest({SnapshotServiceImpl.class})
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class SnapshotServiceImplTest {

@Spy
@InjectMocks
private SnapshotServiceImpl snapshotService = new SnapshotServiceImpl();

@Mock
VolumeDataFactory volFactory;

@Mock
SnapshotDataFactory _snapshotFactory;

@Mock
AsyncCallFuture<SnapshotResult> futureMock;

@Mock
AsyncCallbackDispatcher<SnapshotServiceImpl, CommandResult> caller;

@Before
public void testSetUp() throws Exception {
MockitoAnnotations.initMocks(this);
}

@Test
public void testRevertSnapshotWithNoPrimaryStorageEntry() throws Exception {
SnapshotInfo snapshot = Mockito.mock(SnapshotInfo.class);
VolumeInfo volumeInfo = Mockito.mock(VolumeInfo.class);

Mockito.when(snapshot.getId()).thenReturn(1L);
Mockito.when(snapshot.getVolumeId()).thenReturn(1L);
Mockito.when(_snapshotFactory.getSnapshot(1L, DataStoreRole.Primary)).thenReturn(null);
Mockito.when(volFactory.getVolume(1L, DataStoreRole.Primary)).thenReturn(volumeInfo);

PrimaryDataStore store = Mockito.mock(PrimaryDataStore.class);
Mockito.when(volumeInfo.getDataStore()).thenReturn(store);

PrimaryDataStoreDriver driver = Mockito.mock(PrimaryDataStoreDriver.class);
Mockito.when(store.getDriver()).thenReturn(driver);
Mockito.doNothing().when(driver).revertSnapshot(snapshot, null, caller);

SnapshotResult result = Mockito.mock(SnapshotResult.class);
PowerMockito.whenNew(AsyncCallFuture.class).withNoArguments().thenReturn(futureMock);
Mockito.when(futureMock.get()).thenReturn(result);
Mockito.when(result.isFailed()).thenReturn(false);

Assert.assertEquals(true, snapshotService.revertSnapshot(snapshot));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,24 @@ protected void revertVolumeToSnapshot(SnapshotObjectTO snapshotOnPrimaryStorage,
* and the snapshot path from the primary storage.
*/
protected Pair<String, SnapshotObjectTO> getSnapshot(SnapshotObjectTO snapshotOnPrimaryStorage, SnapshotObjectTO snapshotOnSecondaryStorage,
KVMStoragePool kvmStoragePoolPrimary, KVMStoragePool kvmStoragePoolSecondary){
String snapshotPath = snapshotOnPrimaryStorage.getPath();

if (Files.exists(Paths.get(snapshotPath))) {
return new Pair<>(snapshotPath, snapshotOnPrimaryStorage);
KVMStoragePool kvmStoragePoolPrimary, KVMStoragePool kvmStoragePoolSecondary) {
String snapshotPath = null;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If snapshotOnPrimaryStorage is null, then this is never set?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to set or use it. If the snapshot on primary is not there then we will get it from secondary storage.

if (snapshotOnPrimaryStorage != null) {
snapshotPath = snapshotOnPrimaryStorage.getPath();
if (Files.exists(Paths.get(snapshotPath))) {
return new Pair<>(snapshotPath, snapshotOnPrimaryStorage);
}
}

if (kvmStoragePoolSecondary == null) {
throw new CloudRuntimeException(String.format("Snapshot [%s] does not exists on secondary storage, unable to revert volume [%s] to it.",
snapshotOnSecondaryStorage, snapshotOnSecondaryStorage.getVolume()));
}

s_logger.trace(String.format("Snapshot [%s] does not exists on primary storage [%s], searching snapshot [%s] on secondary storage [%s].", snapshotOnPrimaryStorage,
kvmStoragePoolPrimary, snapshotOnSecondaryStorage, kvmStoragePoolSecondary));
if (s_logger.isTraceEnabled()) {
s_logger.trace(String.format("Snapshot does not exists on primary storage [%s], searching snapshot [%s] on secondary storage [%s].",
kvmStoragePoolPrimary, snapshotOnSecondaryStorage, kvmStoragePoolSecondary));
}

String snapshotPathOnSecondaryStorage = snapshotOnSecondaryStorage.getPath();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ private void deleteTemplate(TemplateInfo templateInfo, long storagePoolId)

/**
* Revert snapshot for a volume
*
* @param snapshotInfo Information about volume snapshot
* @param snapshotOnPrimaryStore Not used
* @throws CloudRuntimeException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.apache.cloudstack.engine.subsystem.api.storage.StorageAction;
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeDataFactory;
import org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo;
import org.apache.cloudstack.framework.async.AsyncCompletionCallback;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
Expand Down Expand Up @@ -121,6 +122,8 @@ public Map<String, String> getCapabilities() {
TemplateManager templateManager;
@Inject
TemplateDataFactory templateDataFactory;
@Inject
VolumeDataFactory volFactory;

@Override
public DataTO getTO(DataObject data) {
Expand Down Expand Up @@ -379,11 +382,21 @@ public void takeSnapshot(SnapshotInfo snapshot, AsyncCompletionCallback<CreateCm

@Override
public void revertSnapshot(SnapshotInfo snapshot, SnapshotInfo snapshotOnPrimaryStore, AsyncCompletionCallback<CommandResult> callback) {
RevertSnapshotCommand cmd = new RevertSnapshotCommand((SnapshotObjectTO)snapshot.getTO(), (SnapshotObjectTO)snapshotOnPrimaryStore.getTO());
SnapshotObjectTO dataOnPrimaryStorage = null;
if (snapshotOnPrimaryStore != null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above if snapshotOnPrimaryStore is null then dataOnPrimaryStorage is never set? (or is it that this will never be null?)

@harikrishna-patnala harikrishna-patnala Jul 6, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be null. We are handling the same case where snapshotOnPrimary is null.
Following is the complete flow

  1. Look for snapshotOnPrimary entry if exists
  2. If exists, use that entry to find the appropriate driver to execute revertSnapshotCommand
  3. If not found, use volume information to find the appropriate driver to execute revertSnapshotCommand
  4. While executing the revertSnapshotCommand, if snapshotOnPrimary exists, use it to revert the volume
  5. If snapshotOnPrimary does not exists, use the snapshot from secondary storage to revert the volume

dataOnPrimaryStorage = (SnapshotObjectTO)snapshotOnPrimaryStore.getTO();
}
RevertSnapshotCommand cmd = new RevertSnapshotCommand((SnapshotObjectTO)snapshot.getTO(), dataOnPrimaryStorage);

CommandResult result = new CommandResult();
try {
EndPoint ep = epSelector.select(snapshotOnPrimaryStore);
EndPoint ep = null;
if (snapshotOnPrimaryStore != null) {
ep = epSelector.select(snapshotOnPrimaryStore);
} else {
VolumeInfo volumeInfo = volFactory.getVolume(snapshot.getVolumeId(), DataStoreRole.Primary);
ep = epSelector.select(volumeInfo);
}
if ( ep == null ){
String errMsg = "No remote endpoint to send RevertSnapshotCommand, check if host or ssvm is down?";
s_logger.error(errMsg);
Expand Down