Java Code Examples for com.alibaba.csp.sentinel.util.AssertUtil#isTrue()

The following examples show how to use com.alibaba.csp.sentinel.util.AssertUtil#isTrue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SentinelApiClient.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
private CompletableFuture<Void> setRulesAsync(String app, String ip, int port, String type, List<? extends RuleEntity> entities) {
    try {
        AssertUtil.notNull(entities, "rules cannot be null");
        AssertUtil.notEmpty(app, "Bad app name");
        AssertUtil.notEmpty(ip, "Bad machine IP");
        AssertUtil.isTrue(port > 0, "Bad machine port");
        String data = JSON.toJSONString(
            entities.stream().map(r -> r.toRule()).collect(Collectors.toList()));
        Map<String, String> params = new HashMap<>(2);
        params.put("type", type);
        params.put("data", data);
        return executeCommand(app, ip, port, SET_RULES_PATH, params, true)
            .thenCompose(r -> {
                if ("success".equalsIgnoreCase(r.trim())) {
                    return CompletableFuture.completedFuture(null);
                }
                return AsyncUtils.newFailedFuture(new CommandFailedException(r));
            });
    } catch (Exception e) {
        logger.error("setRulesAsync API failed, type={}", type, e);
        return AsyncUtils.newFailedFuture(e);
    }
}
 
Example 2
Source File: SentinelApiClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public boolean modifyApis(String app, String ip, int port, List<ApiDefinitionEntity> apis) {
    if (apis == null) {
        return true;
    }

    try {
        AssertUtil.notEmpty(app, "Bad app name");
        AssertUtil.notEmpty(ip, "Bad machine IP");
        AssertUtil.isTrue(port > 0, "Bad machine port");
        String data = JSON.toJSONString(
                apis.stream().map(r -> r.toApiDefinition()).collect(Collectors.toList()));
        Map<String, String> params = new HashMap<>(2);
        params.put("data", data);
        String result = executeCommand(app, ip, port, MODIFY_GATEWAY_API_PATH, params, true).get();
        logger.info("Modify gateway apis: {}", result);
        return true;
    } catch (Exception e) {
        logger.warn("Error when modifying gateway apis", e);
        return false;
    }
}
 
Example 3
Source File: SentinelApiClient.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch all authority rules from provided machine.
 *
 * @param app  application name
 * @param ip   machine client IP
 * @param port machine client port
 * @return all retrieved authority rules
 * @since 0.2.1
 */
public List<AuthorityRuleEntity> fetchAuthorityRulesOfMachine(String app, String ip, int port) {
    AssertUtil.notEmpty(app, "Bad app name");
    AssertUtil.notEmpty(ip, "Bad machine IP");
    AssertUtil.isTrue(port > 0, "Bad machine port");
    Map<String, String> params = new HashMap<>(1);
    params.put("type", AUTHORITY_TYPE);
    List<AuthorityRule> rules = fetchRules(ip, port, AUTHORITY_TYPE, AuthorityRule.class);
    return Optional.ofNullable(rules).map(r -> r.stream()
                .map(e -> AuthorityRuleEntity.fromAuthorityRule(app, ip, port, e))
                .collect(Collectors.toList())
            ).orElse(null);
}
 
Example 4
Source File: RedisConnectionConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Adds port information to the builder. Does only affect Redis URI, cannot be used with Sentinel connections.
 *
 * @param port the port
 * @return the builder
 */
public RedisConnectionConfig.Builder withPort(int port) {

    AssertUtil.assertState(this.host != null, "Host is null. Cannot use in Sentinel mode.");
    AssertUtil.isTrue(isValidPort(port), String.format("Port out of range: %s", port));

    this.port = port;
    return this;
}
 
Example 5
Source File: RedisConnectionConfig.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * Add a withRedisSentinel host/port to the existing builder.
 *
 * @param host the host name
 * @param port the port
 * @return the builder
 */
public RedisConnectionConfig.Builder withRedisSentinel(String host, int port) {

    AssertUtil.assertState(this.host == null, "Cannot use with Redis mode.");
    AssertUtil.notEmpty(host, "Host must not be empty");
    AssertUtil.isTrue(isValidPort(port), String.format("Port out of range: %s", port));

    redisSentinels.add(RedisHostAndPort.of(host, port));
    return this;
}
 
Example 6
Source File: LeapArray.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * The total bucket count is: {@code sampleCount = intervalInMs / windowLengthInMs}.
 *
 * @param sampleCount  bucket count of the sliding window
 * @param intervalInMs the total time interval of this {@link LeapArray} in milliseconds
 */
public LeapArray(int sampleCount, int intervalInMs) {
    AssertUtil.isTrue(sampleCount > 0, "bucket count is invalid: " + sampleCount);
    AssertUtil.isTrue(intervalInMs > 0, "total time interval of the sliding window should be positive");
    AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");

    this.windowLengthInMs = intervalInMs / sampleCount;
    this.intervalInMs = intervalInMs;
    this.sampleCount = sampleCount;

    this.array = new AtomicReferenceArray<>(sampleCount);
}
 
Example 7
Source File: RedisConnectionConfig.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor with host/port and timeout.
 *
 * @param host    the host
 * @param port    the port
 * @param timeout timeout value . unit is mill seconds
 */
public RedisConnectionConfig(String host, int port, long timeout) {

    AssertUtil.notEmpty(host, "Host must not be empty");
    AssertUtil.notNull(timeout, "Timeout duration must not be null");
    AssertUtil.isTrue(timeout >= 0, "Timeout duration must be greater or equal to zero");

    setHost(host);
    setPort(port);
    setTimeout(timeout);
}
 
Example 8
Source File: RedisConnectionConfig.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * Set Sentinel host and port. Creates a new builder.
 *
 * @param host the host name
 * @param port the port
 * @return New builder with Sentinel host/port.
 */
public static RedisConnectionConfig.Builder redisSentinel(String host, int port) {

    AssertUtil.notEmpty(host, "Host must not be empty");
    AssertUtil.isTrue(isValidPort(port), String.format("Port out of range: %s", port));

    RedisConnectionConfig.Builder builder = RedisConnectionConfig.builder();
    return builder.withRedisSentinel(host, port);
}
 
Example 9
Source File: RedisConnectionConfig.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * Set Redis host and port. Creates a new builder
 *
 * @param host the host name
 * @param port the port
 * @return New builder with Redis host/port.
 */
public static RedisConnectionConfig.Builder redis(String host, int port) {

    AssertUtil.notEmpty(host, "Host must not be empty");
    AssertUtil.isTrue(isValidPort(port), String.format("Port out of range: %s", port));

    Builder builder = RedisConnectionConfig.builder();
    return builder.withHost(host).withPort(port);
}
 
Example 10
Source File: SentinelApiClient.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch all authority rules from provided machine.
 *
 * @param app  application name
 * @param ip   machine client IP
 * @param port machine client port
 * @return all retrieved authority rules
 * @since 0.2.1
 */
public List<AuthorityRuleEntity> fetchAuthorityRulesOfMachine(String app, String ip, int port) {
    AssertUtil.notEmpty(app, "Bad app name");
    AssertUtil.notEmpty(ip, "Bad machine IP");
    AssertUtil.isTrue(port > 0, "Bad machine port");
    Map<String, String> params = new HashMap<>(1);
    params.put("type", AUTHORITY_TYPE);
    List<AuthorityRule> rules = fetchRules(ip, port, AUTHORITY_TYPE, AuthorityRule.class);
    return Optional.ofNullable(rules).map(r -> r.stream()
                .map(e -> AuthorityRuleEntity.fromAuthorityRule(app, ip, port, e))
                .collect(Collectors.toList())
            ).orElse(null);
}
 
Example 11
Source File: ClusterAssignServiceImpl.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public ClusterAppAssignResultVO unbindClusterServers(String app, Set<String> machineIdSet) {
    AssertUtil.assertNotBlank(app, "app cannot be blank");
    AssertUtil.isTrue(machineIdSet != null && !machineIdSet.isEmpty(), "machineIdSet cannot be empty");
    ClusterAppAssignResultVO result = new ClusterAppAssignResultVO()
        .setFailedClientSet(new HashSet<>())
        .setFailedServerSet(new HashSet<>());
    for (String machineId : machineIdSet) {
        ClusterAppAssignResultVO resultVO = unbindClusterServer(app, machineId);
        result.getFailedClientSet().addAll(resultVO.getFailedClientSet());
        result.getFailedServerSet().addAll(resultVO.getFailedServerSet());
    }
    return result;
}
 
Example 12
Source File: ClusterServerConfigManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void updateTokenServer(ServerTransportConfig config) {
    int newPort = config.getPort();
    AssertUtil.isTrue(newPort > 0, "token server port should be valid (positive)");
    if (newPort == port) {
        return;
    }
    ClusterServerConfigManager.port = newPort;

    for (ServerTransportConfigObserver observer : TRANSPORT_CONFIG_OBSERVERS) {
        observer.onTransportConfigChange(config);
    }
}
 
Example 13
Source File: LeapArray.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * The total bucket count is: {@code sampleCount = intervalInMs / windowLengthInMs}.
 *
 * @param sampleCount  bucket count of the sliding window
 * @param intervalInMs the total time interval of this {@link LeapArray} in milliseconds
 */
public LeapArray(int sampleCount, int intervalInMs) {
    AssertUtil.isTrue(sampleCount > 0, "bucket count is invalid: " + sampleCount);
    AssertUtil.isTrue(intervalInMs > 0, "total time interval of the sliding window should be positive");
    AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");

    this.windowLengthInMs = intervalInMs / sampleCount;
    this.intervalInMs = intervalInMs;
    this.sampleCount = sampleCount;

    this.array = new AtomicReferenceArray<>(sampleCount);
}
 
Example 14
Source File: EntryConfig.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
public EntryConfig(String resourceName, int resourceType, EntryType entryType, int acquireCount, Object[] args,
                   ContextConfig contextConfig) {
    AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank");
    AssertUtil.notNull(entryType, "entryType cannot be null");
    AssertUtil.isTrue(acquireCount > 0, "acquireCount should be positive");
    this.resourceName = resourceName;
    this.entryType = entryType;
    this.resourceType = resourceType;
    this.acquireCount = acquireCount;
    this.args = args;
    // Constructed ContextConfig should be valid here. Null is allowed here.
    this.contextConfig = contextConfig;
}
 
Example 15
Source File: ClusterParamMetric.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public ClusterParamMetric(int sampleCount, int intervalInMs, int maxCapacity) {
    AssertUtil.isTrue(sampleCount > 0, "sampleCount should be positive");
    AssertUtil.isTrue(intervalInMs > 0, "interval should be positive");
    AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");
    this.metric = new ClusterParameterLeapArray<>(sampleCount, intervalInMs, maxCapacity);
}
 
Example 16
Source File: WebCallbackManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public static void setUrlBlockHandler(UrlBlockHandler urlBlockHandler) {
    AssertUtil.isTrue(urlBlockHandler != null, "URL block handler should not be null");
    WebCallbackManager.urlBlockHandler = urlBlockHandler;
}
 
Example 17
Source File: ClusterParameterLeapArray.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 4 votes vote down vote up
public ClusterParameterLeapArray(int sampleCount, int intervalInMs, int maxCapacity) {
    super(sampleCount, intervalInMs);
    AssertUtil.isTrue(maxCapacity > 0, "maxCapacity of LRU map should be positive");
    this.maxCapacity = maxCapacity;
}
 
Example 18
Source File: WebCallbackManager.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
public static void setUrlBlockHandler(UrlBlockHandler urlBlockHandler) {
    AssertUtil.isTrue(urlBlockHandler != null, "URL block handler should not be null");
    WebCallbackManager.urlBlockHandler = urlBlockHandler;
}
 
Example 19
Source File: RequestLimiter.java    From Sentinel with Apache License 2.0 4 votes vote down vote up
RequestLimiter(LeapArray<LongAdder> data, double qpsAllowed) {
    AssertUtil.isTrue(qpsAllowed >= 0, "max allowed QPS should > 0");
    this.data = data;
    this.qpsAllowed = qpsAllowed;
}
 
Example 20
Source File: RedisHostAndPort.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 2 votes vote down vote up
/**
 * Create a {@link RedisHostAndPort} of {@code host} and {@code port}
 *
 * @param host the hostname
 * @param port a valid port
 * @return the {@link RedisHostAndPort} of {@code host} and {@code port}
 */
public static RedisHostAndPort of(String host, int port) {
    AssertUtil.isTrue(isValidPort(port), String.format("Port out of range: %s", port));
    return new RedisHostAndPort(host, port);
}