use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class PartitionStateManager method initializePartitionAssignments.
boolean initializePartitionAssignments(Set<Address> excludedAddresses) {
if (!isPartitionAssignmentAllowed()) {
return false;
}
Collection<MemberGroup> memberGroups = createMemberGroups(excludedAddresses);
if (memberGroups.isEmpty()) {
logger.warning("No member group is available to assign partition ownership...");
return false;
}
logger.info("Initializing cluster partition table arrangement...");
Address[][] newState = partitionStateGenerator.arrange(memberGroups, partitions);
if (newState.length != partitionCount) {
throw new HazelcastException("Invalid partition count! " + "Expected: " + partitionCount + ", Actual: " + newState.length);
}
// increment state version to make fail cluster state transaction
// if it's started and not locked the state yet.
stateVersion.incrementAndGet();
ClusterState clusterState = node.getClusterService().getClusterState();
if (clusterState != ClusterState.ACTIVE) {
// cluster state is either changed or locked, decrement version back and fail.
stateVersion.decrementAndGet();
logger.warning("Partitions can't be assigned since cluster-state= " + clusterState);
return false;
}
for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
InternalPartitionImpl partition = partitions[partitionId];
Address[] replicas = newState[partitionId];
partition.setReplicaAddresses(replicas);
}
setInitialized();
return true;
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class IOUtil method copyFile.
/**
* Copies source file to target and creates the target if necessary. The target can be a directory or file. If the target
* is a file, nests the new file under the target directory, otherwise copies to the given target.
*
* @param source the source file
* @param target the destination file or directory
* @param sourceCount The maximum number of bytes to be transferred. If negative transfers the entire source file.
* @throws IllegalArgumentException if the source was not found or the source not a file
* @throws HazelcastException if there was any exception while creating directories or copying
*/
public static void copyFile(File source, File target, long sourceCount) {
if (!source.exists()) {
throw new IllegalArgumentException("Source does not exist");
}
if (!source.isFile()) {
throw new IllegalArgumentException("Source is not a file");
}
if (!target.exists() && !target.mkdirs()) {
throw new HazelcastException("Could not create the target directory " + target);
}
final File destination = target.isDirectory() ? new File(target, source.getName()) : target;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream(source);
out = new FileOutputStream(destination);
final FileChannel inChannel = in.getChannel();
final FileChannel outChannel = out.getChannel();
final long transferCount = sourceCount > 0 ? sourceCount : inChannel.size();
inChannel.transferTo(0, transferCount, outChannel);
} catch (Exception e) {
throw new HazelcastException("Error occurred while copying", e);
} finally {
closeResource(in);
closeResource(out);
}
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class IOUtilTest method testCopyFileFailsWhenTargetDoesntExistAndCannotBeCreated.
@Test
public void testCopyFileFailsWhenTargetDoesntExistAndCannotBeCreated() throws IOException {
final File target = mock(File.class);
when(target.exists()).thenReturn(false);
when(target.mkdirs()).thenReturn(false);
final File source = new File("source");
assertTrue(!source.exists());
source.createNewFile();
try {
copyFile(source, target, -1);
fail();
} catch (HazelcastException expected) {
EmptyStatement.ignore(expected);
}
delete(source);
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class IOUtilTest method testCopyFailsWhenSourceCannotBeListed.
@Test
public void testCopyFailsWhenSourceCannotBeListed() throws IOException {
final File source = mock(File.class);
when(source.exists()).thenReturn(true);
when(source.isDirectory()).thenReturn(true);
when(source.listFiles()).thenReturn(null);
when(source.getName()).thenReturn("dummy");
final File dest = new File("dest");
assertTrue(!dest.exists());
dest.mkdir();
try {
copy(source, dest);
fail();
} catch (HazelcastException expected) {
EmptyStatement.ignore(expected);
}
delete(dest);
}
use of com.hazelcast.core.HazelcastException in project hazelcast by hazelcast.
the class ExceptionUtilTest method testPeel_whenThrowableIsExecutionExceptionWithNullCause_thenReturnHazelcastException.
@Test
public void testPeel_whenThrowableIsExecutionExceptionWithNullCause_thenReturnHazelcastException() {
ExecutionException exception = new ExecutionException(null);
RuntimeException result = ExceptionUtil.peel(exception);
assertTrue(result instanceof HazelcastException);
assertEquals(exception, result.getCause());
}
Aggregations