[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-03-22 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r176441369
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/Xenserver625StorageProcessor.java
 ##
 @@ -65,90 +69,181 @@ public Xenserver625StorageProcessor(final 
CitrixResourceBase resource) {
 super(resource);
 }
 
-protected boolean mountNfs(final Connection conn, final String remoteDir, 
String localDir) {
+private void mountNfs(Connection conn, String remoteDir, String localDir) {
 if (localDir == null) {
 localDir = "/var/cloud_mount/" + 
UUID.nameUUIDFromBytes(remoteDir.getBytes());
 }
-
-final String results = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir",
-remoteDir);
-
-if (results == null || results.isEmpty()) {
+String result = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir", remoteDir);
+if (StringUtils.isBlank(result)) {
 final String errMsg = "Could not mount secondary storage " + 
remoteDir + " on host " + localDir;
-
 s_logger.warn(errMsg);
-
 throw new CloudRuntimeException(errMsg);
 }
-
-return true;
 }
 
-protected boolean makeDirectory(final Connection conn, final String path) {
-final String result = hypervisorResource.callHostPlugin(conn, 
"cloud-plugin-storage", "makeDirectory", "path", path);
+protected boolean makeDirectory(Connection conn, String path) {
+String result = hypervisorResource.callHostPlugin(conn, 
"cloud-plugin-storage", "makeDirectory", "path", path);
+return StringUtils.isNotBlank(result);
+}
 
-if (result == null || result.isEmpty()) {
-return false;
+/**
+ *  Creates the file SR for the given path. If there already exist a file 
SR for the path, we return the existing one.
+ *  This method uses a synchronized block to guarantee that only a single 
file SR is created per path.
+ *  If it is not possible to retrieve one file SR or to create one, a 
runtime exception will be thrown.
+ */
+protected SR createFileSR(Connection conn, String path) {
+String srPath = StringUtils.trim(path);
+synchronized (srPath) {
+SR sr = retrieveAlreadyConfiguredSrWithoutException(conn, srPath);
+if (sr == null) {
+sr = createNewFileSr(conn, srPath);
+}
+if (sr == null) {
+String hostUuid = this.hypervisorResource._host.getUuid();
+throw new CloudRuntimeException(String.format("Could not 
retrieve an already used file SR for path [%s] or create a new file SR on host 
[%s]", srPath, hostUuid));
+}
+return sr;
 }
-
-return true;
 }
 
-protected SR createFileSR(final Connection conn, final String path) {
+/**
+ * Creates a new file SR for the given path. If any of XenServer's checked 
exception occurs, we use method {@link #removeSrAndPbdIfPossible(Connection, 
SR, PBD)} to clean the created PBD and SR entries.
+ * To avoid race conditions between management servers, we are using a 
deterministic srUuid for the file SR to be created (we are leaving XenServer 
with the burden of managing race conditions). The UUID is based on the SR file 
path, and is generated using {@link UUID#nameUUIDFromBytes(byte[])}.
+ * If there is an SR with the generated UUID, this means that some other 
management server has just created it. An exception will occur and this 
exception will be an {@link InternalError}. The exception will contain {@link 
InternalError#message} a message saying 
'Db_exn.Uniqueness_constraint_violation'.
+ * For cases where the previous described error happens, we catch the 
exception and use the method {@link 
#retrieveAlreadyConfiguredSrWithoutException(Connection, String)}.
+ */
+protected SR createNewFileSr(Connection conn, String srPath) {
+String hostUuid = hypervisorResource.getHost().getUuid();
+s_logger.debug(String.format("Creating file SR for path [%s] on host 
[%s]", srPath, this.hypervisorResource._host.getUuid()));
 SR sr = null;
 PBD pbd = null;
-
 try {
-final String srname = path.trim();
-synchronized (srname.intern()) {
-final Set srs = SR.getByNameLabel(conn, srname);
-if (srs != null && !srs.isEmpty()) {
-return srs.iterator().next();
-}
-final Map smConfig = new HashMap();
-final Host host = Host.getByUuid(conn, 
hypervisorResour

[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-03-13 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r174255133
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/Xenserver625StorageProcessor.java
 ##
 @@ -65,90 +69,181 @@ public Xenserver625StorageProcessor(final 
CitrixResourceBase resource) {
 super(resource);
 }
 
-protected boolean mountNfs(final Connection conn, final String remoteDir, 
String localDir) {
+private void mountNfs(Connection conn, String remoteDir, String localDir) {
 if (localDir == null) {
 localDir = "/var/cloud_mount/" + 
UUID.nameUUIDFromBytes(remoteDir.getBytes());
 }
-
-final String results = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir",
-remoteDir);
-
-if (results == null || results.isEmpty()) {
+String result = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir", remoteDir);
+if (StringUtils.isBlank(result)) {
 final String errMsg = "Could not mount secondary storage " + 
remoteDir + " on host " + localDir;
-
 s_logger.warn(errMsg);
-
 throw new CloudRuntimeException(errMsg);
 }
-
-return true;
 }
 
-protected boolean makeDirectory(final Connection conn, final String path) {
-final String result = hypervisorResource.callHostPlugin(conn, 
"cloud-plugin-storage", "makeDirectory", "path", path);
+protected boolean makeDirectory(Connection conn, String path) {
 
 Review comment:
   Because it is useless in this context.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-03-13 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r174254978
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/Xenserver625StorageProcessor.java
 ##
 @@ -65,90 +69,181 @@ public Xenserver625StorageProcessor(final 
CitrixResourceBase resource) {
 super(resource);
 }
 
-protected boolean mountNfs(final Connection conn, final String remoteDir, 
String localDir) {
+private void mountNfs(Connection conn, String remoteDir, String localDir) {
 if (localDir == null) {
 localDir = "/var/cloud_mount/" + 
UUID.nameUUIDFromBytes(remoteDir.getBytes());
 }
-
-final String results = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir",
-remoteDir);
-
-if (results == null || results.isEmpty()) {
+String result = hypervisorResource.callHostPluginAsync(conn, 
"cloud-plugin-storage", "mountNfsSecondaryStorage", 100 * 1000, "localDir", 
localDir, "remoteDir", remoteDir);
 
 Review comment:
   Of course, they are. However, here and in a lot of places in ACS, there is 
an indiscriminate use of the final keyword. It does not harm, but it is 
something I have to read when reviewing/coding, which does not aggregate/help. 
So, I remove when they are not needed.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-01-30 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r164820107
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/test/java/com/cloud/hypervisor/xenserver/resource/Xenserver625StorageProcessorTest.java
 ##
 @@ -0,0 +1,459 @@
+/*
+ * 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 com.cloud.hypervisor.xenserver.resource;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.times;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.UUID;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.xmlrpc.XmlRpcException;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InOrder;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import com.cloud.utils.exception.CloudRuntimeException;
+import com.xensource.xenapi.Connection;
+import com.xensource.xenapi.Host;
+import com.xensource.xenapi.PBD;
+import com.xensource.xenapi.PBD.Record;
+import com.xensource.xenapi.SR;
+import com.xensource.xenapi.Types.InternalError;
+import com.xensource.xenapi.Types.XenAPIException;
+
+@RunWith(PowerMockRunner.class)
+public class Xenserver625StorageProcessorTest {
+
+@InjectMocks
+private Xenserver625StorageProcessor xenserver625StorageProcessor;
+
+@Mock
+private CitrixResourceBase citrixResourceBase;
+
+@Mock
+private Connection connectionMock;
+
+private String pathMock = "pathMock";
+
+@Before
+public void before() {
+xenserver625StorageProcessor = Mockito.spy(new 
Xenserver625StorageProcessor(citrixResourceBase));
+citrixResourceBase._host = Mockito.mock(XsHost.class);
+
Mockito.when(citrixResourceBase.getHost()).thenReturn(citrixResourceBase._host);
+}
+
+@Test
+public void makeDirectoryTestCallHostPlugingReturningEmpty() {
+boolean makeDirectoryReturn = 
configureAndExecuteMakeDirectoryMethodForTest(pathMock, StringUtils.EMPTY);
+
+assertFalse(makeDirectoryReturn);
+}
+
+@Test
+public void makeDirectoryTestCallHostPlugingReturningNull() {
+boolean makeDirectoryReturn = 
configureAndExecuteMakeDirectoryMethodForTest(pathMock, null);
+
+assertFalse(makeDirectoryReturn);
+}
+
+@Test
+public void makeDirectoryTestCallHostPlugingReturningSomething() {
+boolean makeDirectoryReturn = 
configureAndExecuteMakeDirectoryMethodForTest(pathMock, "Soemthing");
 
 Review comment:
   Ah this one. It can actually be any string. instead of writing a fictitious 
directory, which though it would be misleading. Therefore, I decided to write 
anything... I will try to change this...


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-01-30 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r164819588
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/test/java/com/cloud/hypervisor/xenserver/resource/XenServer625ResourceTest.java
 ##
 @@ -30,7 +31,7 @@
 
 @Before
 public void beforeTest() {
-super.citrixResourceBase = new Xenserver625Resource();
+super.citrixResourceBase = Mockito.spy(new Xenserver625Resource());
 
 Review comment:
   Oh, this one... It is because there is a hierarchy of test classes.. :(.. 
the property `citrixResourceBase` needs to be either a mock or spy object 
because of the testes executed in the super class. Therefore, needed to create 
a spy object whenever attributing an object to this property.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] rafaelweingartner commented on a change in pull request #2414: [CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools

2018-01-30 Thread GitBox
rafaelweingartner commented on a change in pull request #2414: 
[CLOUDSTACK-10241] Duplicated file SRs being created in XenServer pools
URL: https://github.com/apache/cloudstack/pull/2414#discussion_r164818463
 
 

 ##
 File path: 
plugins/hypervisors/xenserver/src/main/java/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java
 ##
 @@ -17,7 +17,52 @@
 package com.cloud.hypervisor.xenserver.resource;
 
 Review comment:
   Sorry for that. It is a small change at lien 244. I removed a final keyword 
so I would be able to Spy an object to create unit test cases. I removed the 
formatting. It is my Eclipse, even though I configured it to format only edited 
lines, it keeps applying the standard on everything.


This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services