StringhostToBind= getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_BIND); if (hostToBind != null && hostToBind.length() > 0 && isInvalidLocalHost(hostToBind)) { thrownewIllegalArgumentException("Specified invalid bind ip from property:" + Constants.DUBBO_IP_TO_BIND + ", value:" + hostToBind); }
// if bind ip is not found in environment, keep looking up if (hostToBind == null || hostToBind.length() == 0) { hostToBind = protocolConfig.getHost(); if (provider != null && (hostToBind == null || hostToBind.length() == 0)) { hostToBind = provider.getHost(); } if (isInvalidLocalHost(hostToBind)) { anyhost = true; try { hostToBind = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { logger.warn(e.getMessage(), e); } if (isInvalidLocalHost(hostToBind)) { if (registryURLs != null && !registryURLs.isEmpty()) { for (URL registryURL : registryURLs) { if (Constants.MULTICAST.equalsIgnoreCase(registryURL.getParameter("registry"))) { // skip multicast registry since we cannot connect to it via Socket continue; } try { Socketsocket=newSocket(); try { SocketAddressaddr=newInetSocketAddress(registryURL.getHost(), registryURL.getPort()); socket.connect(addr, 1000); hostToBind = socket.getLocalAddress().getHostAddress(); break; } finally { try { socket.close(); } catch (Throwable e) { } } } catch (Exception e) { logger.warn(e.getMessage(), e); } } } if (isInvalidLocalHost(hostToBind)) { hostToBind = getLocalHost(); } } } }
map.put(Constants.BIND_IP_KEY, hostToBind);
// registry ip is not used for bind ip by default StringhostToRegistry= getValueFromConfig(protocolConfig, Constants.DUBBO_IP_TO_REGISTRY); if (hostToRegistry != null && hostToRegistry.length() > 0 && isInvalidLocalHost(hostToRegistry)) { thrownewIllegalArgumentException("Specified invalid registry ip from property:" + Constants.DUBBO_IP_TO_REGISTRY + ", value:" + hostToRegistry); } elseif (hostToRegistry == null || hostToRegistry.length() == 0) { // bind ip is used as registry ip by default hostToRegistry = hostToBind; }
You are given an integer array nums where the largest integer is unique.
Determine whether the largest element in the array is at least twice as much as every other number in the array. If it is, return the index of the largest element, or return -1 otherwise. 确认在数组中的最大数是否是其余任意数的两倍大及以上,如果是返回索引,如果不是返回-1
示例
Example 1:
Input: nums = [3,6,1,0] Output: 1 Explanation: 6 is the largest integer. For every other number in the array x, 6 is at least twice as big as x. The index of value 6 is 1, so we return 1.
Example 2:
Input: nums = [1,2,3,4] Output: -1 Explanation: 4 is less than twice the value of 3, so we return -1.
EventHandlerGroup<T> createEventProcessors( final Sequence[] barrierSequences, final EventHandler<? super T>[] eventHandlers) { checkNotStarted();
final Sequence[] processorSequences = newSequence[eventHandlers.length]; finalSequenceBarrierbarrier= ringBuffer.newBarrier(barrierSequences);
for (inti=0, eventHandlersLength = eventHandlers.length; i < eventHandlersLength; i++) { final EventHandler<? super T> eventHandler = eventHandlers[i];
publicvoidrun() { if (running.compareAndSet(IDLE, RUNNING)) { sequenceBarrier.clearAlert();
notifyStart(); try { if (running.get() == RUNNING) { processEvents(); } } finally { notifyShutdown(); running.set(IDLE); } } else { // This is a little bit of guess work. The running state could of changed to HALTED by // this point. However, Java does not have compareAndExchange which is the only way // to get it exactly correct. if (running.get() == RUNNING) { thrownewIllegalStateException("Thread is already running"); } else { earlyExit(); } } }
Map<String, Object> propertySourcesPlaceholderPropertyValues = newHashMap<>(); // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer propertySourcesPlaceholderPropertyValues.put("order", 0);
LocalFileConfigRepository createLocalConfigRepository(String namespace) { if (m_configUtil.isInLocalMode()) { logger.warn( "==== Apollo is in local mode! Won't pull configs from remote server for namespace {} ! ====", namespace); returnnewLocalFileConfigRepository(namespace); } // 正常会走这个,因为要从配置中心获取 returnnewLocalFileConfigRepository(namespace, createRemoteConfigRepository(namespace)); }
com.ctrip.framework.apollo.internals.RemoteConfigRepository#loadApolloConfig private ApolloConfig loadApolloConfig() { if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) { //wait at most 5 seconds try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException e) { } } StringappId= m_configUtil.getAppId(); Stringcluster= m_configUtil.getCluster(); StringdataCenter= m_configUtil.getDataCenter(); Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace)); intmaxRetries= m_configNeedForceRefresh.get() ? 2 : 1; longonErrorSleepTime=0; // 0 means no sleep Throwableexception=null;
// 获取配置 List<ServiceDTO> configServices = getConfigServices(); Stringurl=null; for (inti=0; i < maxRetries; i++) { List<ServiceDTO> randomConfigServices = Lists.newLinkedList(configServices); Collections.shuffle(randomConfigServices); //Access the server which notifies the client first if (m_longPollServiceDto.get() != null) { randomConfigServices.add(0, m_longPollServiceDto.getAndSet(null)); }
for (ServiceDTO configService : randomConfigServices) { if (onErrorSleepTime > 0) { logger.warn( "Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
if (response.getStatusCode() == 304) { logger.debug("Config server responds with 304 HTTP status code."); return m_configCache.get(); }
ApolloConfigresult= response.getBody();
logger.debug("Loaded config for {}: {}", m_namespace, result);
return result; } catch (ApolloConfigStatusCodeException ex) { ApolloConfigStatusCodeExceptionstatusCodeException= ex; //config not found if (ex.getStatusCode() == 404) { Stringmessage= String.format( "Could not find config for namespace - appId: %s, cluster: %s, namespace: %s, " + "please check whether the configs are released in Apollo!", appId, cluster, m_namespace); statusCodeException = newApolloConfigStatusCodeException(ex.getStatusCode(), message); } Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(statusCodeException)); transaction.setStatus(statusCodeException); exception = statusCodeException; } catch (Throwable ex) { Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex)); transaction.setStatus(ex); exception = ex; } finally { transaction.complete(); }
// if force refresh, do normal sleep, if normal config load, do exponential sleep onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() : m_loadConfigFailSchedulePolicy.fail(); }
com.ctrip.framework.apollo.internals.RemoteConfigRepository#getConfigServices private List<ServiceDTO> getConfigServices() { List<ServiceDTO> services = m_serviceLocator.getConfigServices(); if (services.size() == 0) { thrownewApolloConfigException("No available config service"); }
return services; }
1 2 3 4 5 6 7 8
com.ctrip.framework.apollo.internals.ConfigServiceLocator#getConfigServices public List<ServiceDTO> getConfigServices() { if (m_configServices.get().isEmpty()) { updateConfigServices(); }