use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.
the class LuceneCreateIndexFunction method execute.
public void execute(final FunctionContext context) {
String memberId = null;
try {
final LuceneIndexInfo indexInfo = (LuceneIndexInfo) context.getArguments();
final Cache cache = getCache();
memberId = cache.getDistributedSystem().getDistributedMember().getId();
LuceneService service = LuceneServiceProvider.get(cache);
INDEX_NAME.validateName(indexInfo.getIndexName());
String[] fields = indexInfo.getSearchableFieldNames();
String[] analyzerName = indexInfo.getFieldAnalyzers();
final LuceneIndexFactory indexFactory = service.createIndexFactory();
if (analyzerName == null || analyzerName.length == 0) {
for (String field : fields) {
indexFactory.addField(field);
}
} else {
if (analyzerName.length != fields.length)
throw new Exception("Mismatch in lengths of fields and analyzers");
for (int i = 0; i < fields.length; i++) {
Analyzer analyzer = toAnalyzer(analyzerName[i]);
indexFactory.addField(fields[i], analyzer);
}
}
REGION_PATH.validateName(indexInfo.getRegionPath());
indexFactory.create(indexInfo.getIndexName(), indexInfo.getRegionPath());
// TODO - update cluster configuration by returning a valid XmlEntity
XmlEntity xmlEntity = null;
context.getResultSender().lastResult(new CliFunctionResult(memberId, xmlEntity));
} catch (Exception e) {
String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
}
}
use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.
the class ClusterConfigurationServiceDUnitTest method testSharedConfigurationService.
@Test
public void testSharedConfigurationService() throws Exception {
// Start the Locator and wait for shared configuration to be available
final String testGroup = "G1";
final String clusterLogLevel = "error";
final String groupLogLevel = "fine";
final String testName = getName();
final VM locator1Vm = getHost(0).getVM(1);
final VM dataMemberVm = getHost(0).getVM(2);
final VM locator2Vm = getHost(0).getVM(3);
final int[] ports = getRandomAvailableTCPPorts(3);
final int locator1Port = ports[0];
locator1Vm.invoke(() -> {
final File locatorLogFile = new File(testName + "-locator-" + locator1Port + ".log");
final Properties locatorProps = new Properties();
locatorProps.setProperty(NAME, "Locator1");
locatorProps.setProperty(MCAST_PORT, "0");
locatorProps.setProperty(LOG_LEVEL, "info");
locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
try {
final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locator1Port, locatorLogFile, null, locatorProps);
WaitCriterion wc = new WaitCriterion() {
@Override
public boolean done() {
return locator.isSharedConfigurationRunning();
}
@Override
public String description() {
return "Waiting for shared configuration to be started";
}
};
waitForCriterion(wc, TIMEOUT, INTERVAL, true);
} catch (IOException e) {
fail("Unable to create a locator with a shared configuration", e);
}
});
XmlEntity xmlEntity = dataMemberVm.invoke(() -> {
Properties localProps = new Properties();
localProps.setProperty(MCAST_PORT, "0");
localProps.setProperty(LOCATORS, "localhost[" + locator1Port + "]");
localProps.setProperty(GROUPS, testGroup);
getSystem(localProps);
Cache cache = getCache();
assertNotNull(cache);
DiskStoreFactory dsFactory = cache.createDiskStoreFactory();
File dsDir = new File("dsDir");
if (!dsDir.exists()) {
dsDir.mkdir();
}
dsFactory.setDiskDirs(new File[] { dsDir });
dsFactory.create(DISKSTORENAME);
RegionFactory regionFactory = getCache().createRegionFactory(RegionShortcut.REPLICATE);
regionFactory.create(REGION1);
return new XmlEntity(CacheXml.REGION, "name", REGION1);
});
locator1Vm.invoke(() -> {
ClusterConfigurationService sc = InternalLocator.getLocator().getSharedConfiguration();
sc.addXmlEntity(xmlEntity, new String[] { testGroup });
// Modify property and cache attributes
Properties clusterProperties = new Properties();
clusterProperties.setProperty(LOG_LEVEL, clusterLogLevel);
XmlEntity cacheEntity = XmlEntity.builder().withType(CacheXml.CACHE).build();
Map<String, String> cacheAttributes = new HashMap<String, String>();
cacheAttributes.put(CacheXml.COPY_ON_READ, "true");
sc.modifyXmlAndProperties(clusterProperties, cacheEntity, null);
clusterProperties.setProperty(LOG_LEVEL, groupLogLevel);
sc.modifyXmlAndProperties(clusterProperties, cacheEntity, new String[] { testGroup });
// Add a jar
byte[][] jarBytes = new byte[1][];
jarBytes[0] = "Hello".getBytes();
assertTrue(sc.addJarsToThisLocator(new String[] { "foo.jar" }, jarBytes, null));
// Add a jar for the group
jarBytes = new byte[1][];
jarBytes[0] = "Hello".getBytes();
assertTrue(sc.addJarsToThisLocator(new String[] { "bar.jar" }, jarBytes, new String[] { testGroup }));
});
final int locator2Port = ports[1];
// Create another locator in VM2
locator2Vm.invoke(() -> {
final File locatorLogFile = new File(testName + "-locator-" + locator2Port + ".log");
final Properties locatorProps = new Properties();
locatorProps.setProperty(NAME, "Locator2");
locatorProps.setProperty(MCAST_PORT, "0");
locatorProps.setProperty(LOG_LEVEL, "info");
locatorProps.setProperty(ENABLE_CLUSTER_CONFIGURATION, "true");
locatorProps.setProperty(LOCATORS, "localhost[" + locator1Port + "]");
try {
final InternalLocator locator = (InternalLocator) Locator.startLocatorAndDS(locator2Port, locatorLogFile, null, locatorProps);
WaitCriterion wc = new WaitCriterion() {
@Override
public boolean done() {
return locator.isSharedConfigurationRunning();
}
@Override
public String description() {
return "Waiting for shared configuration to be started";
}
};
waitForCriterion(wc, TIMEOUT, INTERVAL, true);
} catch (IOException e) {
fail("Unable to create a locator with a shared configuration", e);
}
InternalLocator locator = (InternalLocator) Locator.getLocator();
ClusterConfigurationService sharedConfig = locator.getSharedConfiguration();
Map<String, Configuration> entireConfiguration = sharedConfig.getEntireConfiguration();
Configuration clusterConfig = entireConfiguration.get(ClusterConfigurationService.CLUSTER_CONFIG);
assertNotNull(clusterConfig);
assertNotNull(clusterConfig.getJarNames());
assertTrue(clusterConfig.getJarNames().contains("foo.jar"));
assertTrue(clusterConfig.getGemfireProperties().getProperty(LOG_LEVEL).equals(clusterLogLevel));
assertNotNull(clusterConfig.getCacheXmlContent());
Configuration testGroupConfiguration = entireConfiguration.get(testGroup);
assertNotNull(testGroupConfiguration);
assertNotNull(testGroupConfiguration.getJarNames());
assertTrue(testGroupConfiguration.getJarNames().contains("bar.jar"));
assertTrue(testGroupConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(groupLogLevel));
assertNotNull(testGroupConfiguration.getCacheXmlContent());
assertTrue(testGroupConfiguration.getCacheXmlContent().contains(REGION1));
Map<String, byte[]> jarData = sharedConfig.getAllJarsFromThisLocator(entireConfiguration.keySet());
String[] jarNames = jarData.keySet().stream().toArray(String[]::new);
byte[][] jarBytes = jarData.values().toArray(new byte[jarNames.length][]);
assertNotNull(jarNames);
assertNotNull(jarBytes);
sharedConfig.deleteXmlEntity(new XmlEntity(CacheXml.REGION, "name", REGION1), new String[] { testGroup });
sharedConfig.removeJars(new String[] { "foo.jar" }, null);
sharedConfig.removeJars(null, null);
});
dataMemberVm.invoke(() -> {
Set<String> groups = new HashSet<String>();
groups.add(testGroup);
ConfigurationRequest configRequest = new ConfigurationRequest(groups);
ConfigurationResponse configResponse = (ConfigurationResponse) new TcpClient().requestToServer(InetAddress.getByName("localhost"), locator2Port, configRequest, 1000);
assertNotNull(configResponse);
Map<String, Configuration> requestedConfiguration = configResponse.getRequestedConfiguration();
Configuration clusterConfiguration = requestedConfiguration.get(ClusterConfigurationService.CLUSTER_CONFIG);
assertNotNull(clusterConfiguration);
assertTrue(configResponse.getJarNames().length == 0);
assertTrue(configResponse.getJars().length == 0);
assertTrue(clusterConfiguration.getJarNames().isEmpty());
assertTrue(clusterConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(clusterLogLevel));
Configuration testGroupConfiguration = requestedConfiguration.get(testGroup);
assertNotNull(testGroupConfiguration);
assertFalse(testGroupConfiguration.getCacheXmlContent().contains(REGION1));
assertTrue(testGroupConfiguration.getJarNames().isEmpty());
assertTrue(testGroupConfiguration.getGemfireProperties().getProperty(LOG_LEVEL).equals(groupLogLevel));
GemFireCacheImpl cache = (GemFireCacheImpl) getCache();
Map<InternalDistributedMember, Collection<String>> locatorsWithSharedConfiguration = cache.getDistributionManager().getAllHostedLocatorsWithSharedConfiguration();
assertFalse(locatorsWithSharedConfiguration.isEmpty());
assertTrue(locatorsWithSharedConfiguration.size() == 2);
Set<InternalDistributedMember> locatorMembers = locatorsWithSharedConfiguration.keySet();
for (InternalDistributedMember locatorMember : locatorMembers) {
System.out.println(locatorMember);
}
return null;
});
}
use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.
the class LuceneCreateIndexFunctionJUnitTest method prepare.
@Before
public void prepare() {
cache = Fakes.cache();
DistributedSystem ds = Fakes.distributedSystem();
member = ds.getDistributedMember().getId();
service = mock(InternalLuceneService.class);
when(cache.getService(InternalLuceneService.class)).thenReturn(service);
factory = mock(LuceneIndexFactory.class);
when(service.createIndexFactory()).thenReturn(factory);
context = mock(FunctionContext.class);
resultSender = mock(ResultSender.class);
when(context.getResultSender()).thenReturn(resultSender);
XmlEntity xmlEntity = null;
expectedResult = new CliFunctionResult(member, xmlEntity);
}
use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.
the class DiskStoreCommands method destroyDiskStore.
@CliCommand(value = CliStrings.DESTROY_DISK_STORE, help = CliStrings.DESTROY_DISK_STORE__HELP)
@CliMetaData(shellOnly = false, relatedTopic = { CliStrings.TOPIC_GEODE_DISKSTORE })
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result destroyDiskStore(@CliOption(key = CliStrings.DESTROY_DISK_STORE__NAME, mandatory = true, help = CliStrings.DESTROY_DISK_STORE__NAME__HELP) String name, @CliOption(key = CliStrings.DESTROY_DISK_STORE__GROUP, help = CliStrings.DESTROY_DISK_STORE__GROUP__HELP, optionContext = ConverterHint.MEMBERGROUP) String[] groups) {
try {
TabularResultData tabularData = ResultBuilder.createTabularResultData();
boolean accumulatedData = false;
Set<DistributedMember> targetMembers = CliUtil.findMembers(groups, null);
if (targetMembers.isEmpty()) {
return ResultBuilder.createUserErrorResult(CliStrings.NO_MEMBERS_FOUND_MESSAGE);
}
ResultCollector<?, ?> rc = CliUtil.executeFunction(new DestroyDiskStoreFunction(), new Object[] { name }, targetMembers);
List<CliFunctionResult> results = CliFunctionResult.cleanResults((List<?>) rc.getResult());
AtomicReference<XmlEntity> xmlEntity = new AtomicReference<>();
for (CliFunctionResult result : results) {
if (result.getThrowable() != null) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Result", "ERROR: " + result.getThrowable().getClass().getName() + ": " + result.getThrowable().getMessage());
accumulatedData = true;
tabularData.setStatus(Status.ERROR);
} else if (result.getMessage() != null) {
tabularData.accumulate("Member", result.getMemberIdOrName());
tabularData.accumulate("Result", result.getMessage());
accumulatedData = true;
if (xmlEntity.get() == null) {
xmlEntity.set(result.getXmlEntity());
}
}
}
if (!accumulatedData) {
return ResultBuilder.createInfoResult("No matching disk stores found.");
}
Result result = ResultBuilder.buildResult(tabularData);
if (xmlEntity.get() != null) {
persistClusterConfiguration(result, () -> getSharedConfiguration().deleteXmlEntity(xmlEntity.get(), groups));
}
return result;
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable th) {
SystemFailure.checkFailure();
return ResultBuilder.createGemFireErrorResult(CliStrings.format(CliStrings.DESTROY_DISK_STORE__ERROR_WHILE_DESTROYING_REASON_0, new Object[] { th.getMessage() }));
}
}
use of org.apache.geode.management.internal.configuration.domain.XmlEntity in project geode by apache.
the class PDXCommands method configurePDX.
@CliCommand(value = CliStrings.CONFIGURE_PDX, help = CliStrings.CONFIGURE_PDX__HELP)
@CliMetaData(relatedTopic = CliStrings.TOPIC_GEODE_REGION)
@ResourceOperation(resource = Resource.DATA, operation = Operation.MANAGE)
public Result configurePDX(@CliOption(key = CliStrings.CONFIGURE_PDX__READ__SERIALIZED, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__READ__SERIALIZED__HELP) Boolean readSerialized, @CliOption(key = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS__HELP) Boolean ignoreUnreadFields, @CliOption(key = CliStrings.CONFIGURE_PDX__DISKSTORE, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = "", help = CliStrings.CONFIGURE_PDX__DISKSTORE__HELP) String diskStore, @CliOption(key = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__AUTO__SERIALIZER__CLASSES__HELP) String[] patterns, @CliOption(key = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES, unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, specifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, help = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES__HELP) String[] portablePatterns) {
Result result = null;
try {
InfoResultData ird = ResultBuilder.createInfoResultData();
CacheCreation cache = new CacheCreation(true);
if ((portablePatterns != null && portablePatterns.length > 0) && (patterns != null && patterns.length > 0)) {
return ResultBuilder.createUserErrorResult(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE);
}
if (!CliUtil.getAllNormalMembers(CliUtil.getCacheIfExists()).isEmpty()) {
ird.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING);
}
// Set persistent and the disk-store
if (diskStore != null) {
cache.setPdxPersistent(true);
ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent());
if (!diskStore.equals("")) {
cache.setPdxDiskStore(diskStore);
ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + cache.getPdxDiskStore());
} else {
ird.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + "DEFAULT");
}
} else {
cache.setPdxPersistent(CacheConfig.DEFAULT_PDX_PERSISTENT);
ird.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = " + cache.getPdxPersistent());
}
// Set read-serialized
if (readSerialized != null) {
cache.setPdxReadSerialized(readSerialized);
} else {
cache.setPdxReadSerialized(CacheConfig.DEFAULT_PDX_READ_SERIALIZED);
}
ird.addLine(CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + cache.getPdxReadSerialized());
// Set ingoreUnreadFields
if (ignoreUnreadFields != null) {
cache.setPdxIgnoreUnreadFields(ignoreUnreadFields);
} else {
cache.setPdxIgnoreUnreadFields(CacheConfig.DEFAULT_PDX_IGNORE_UNREAD_FIELDS);
}
ird.addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " + cache.getPdxIgnoreUnreadFields());
if (portablePatterns != null) {
ReflectionBasedAutoSerializer autoSerializer = new ReflectionBasedAutoSerializer(portablePatterns);
cache.setPdxSerializer(autoSerializer);
ird.addLine("PDX Serializer " + cache.getPdxSerializer().getClass().getName());
ird.addLine("Portable classes " + Arrays.toString(portablePatterns));
}
if (patterns != null) {
ReflectionBasedAutoSerializer nonPortableAutoSerializer = new ReflectionBasedAutoSerializer(true, patterns);
cache.setPdxSerializer(nonPortableAutoSerializer);
ird.addLine("PDX Serializer : " + cache.getPdxSerializer().getClass().getName());
ird.addLine("Non portable classes :" + Arrays.toString(patterns));
}
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
CacheXmlGenerator.generate(cache, printWriter, true, false, false);
printWriter.close();
String xmlDefinition = stringWriter.toString();
// TODO jbarrett - shouldn't this use the same loadXmlDefinition that other constructors use?
XmlEntity xmlEntity = XmlEntity.builder().withType(CacheXml.PDX).withConfig(xmlDefinition).build();
result = ResultBuilder.buildResult(ird);
persistClusterConfiguration(result, () -> getSharedConfiguration().addXmlEntity(xmlEntity, null));
} catch (Exception e) {
return ResultBuilder.createGemFireErrorResult(e.getMessage());
}
return result;
}
Aggregations