/* * 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.ignite.scenario; import java.io.File; import java.io.IOException; import java.util.Map; import java.util.Random; import org.apache.ignite.IgniteClientDisconnectedException; import org.apache.ignite.IgniteServices; import org.apache.ignite.cluster.ClusterGroup; import org.apache.ignite.scenario.internal.AbstractTask; import org.apache.ignite.scenario.internal.PocServiceImpl; import org.apache.ignite.scenario.internal.PocTesterArguments; import org.apache.ignite.scenario.internal.TaskProperties; import org.apache.ignite.scenario.internal.utils.PocTesterUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Nullable; import static org.apache.ignite.scenario.internal.utils.PocTesterUtils.sleep; /** * Activates cluster. */ public class ServiceTask extends AbstractTask { /** */ private static final Logger LOG = LogManager.getLogger(ServiceTask.class.getName()); /** */ private static final int DFLT_SERVICE_NUM = 500; /** Number of service to deploy */ private int serviceNum; IgniteServices svcs; /** * * @param args Arguments. * @param props Task properties. */ public ServiceTask(PocTesterArguments args, TaskProperties props){ super(args, props); } /** {@inheritDoc} */ @Override public void init() throws IOException { super.init(); serviceNum = props.getInteger("serviceNum", DFLT_SERVICE_NUM); } /** {@inheritDoc} */ @Override public void body0() { String threadName = Thread.currentThread().getName(); deployServices(threadName); cancelServices(threadName); } /** */ private void deployServices(String prefix){ for (int i = 0; i < serviceNum; i++){ // Get an instance of IgniteServices for the cluster group. boolean done = false; int cnt0 = 0; while (!done && cnt0++ < 300) { try { svcs = ignite().services(ignite().cluster().forServers()); } catch (Exception e){ LOG.error(String.format("Failed to get services. Will try again. Cnt = %d", cnt0), e); sleep(100L); } } boolean deployed = false; int cnt = 0; try { while (!deployed && cnt++ < 100) { // Deploy per-node singleton. An instance of the service // will be deployed on every node within the cluster group. svcs.deployNodeSingleton(prefix + "myCounterService" + i, new PocServiceImpl()); deployed = true; } } catch (Exception e){ LOG.error(String.format("Failed to deploy service. Will retry. Cnt = %d", cnt), e); sleep(500L); } } } /** */ private void cancelServices(String prefix){ for (int i = 0; i < serviceNum; i++) svcs.cancel(prefix + "myCounterService" + i); } /** {@inheritDoc} */ @Nullable public String getTaskReport() { //TODO: avoid null result. return null; } /** */ @Override protected void addPropsToMap(TaskProperties props){ super.addPropsToMap(props); Map hdrMap = (Map) propMap.get("headersMap"); hdrMap.put("unit", "boolean"); hdrMap.put("data", "status"); propMap.put("reportDir", "reports"); } }