Skip to content

Commit

Permalink
feat: support spring-boot-actuator (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang committed Aug 2, 2023
1 parent ce0e30d commit 0197f86
Show file tree
Hide file tree
Showing 40 changed files with 806 additions and 31 deletions.
41 changes: 41 additions & 0 deletions cosid-core/src/main/java/me/ahoo/cosid/Decorator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package me.ahoo.cosid;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;

/**
* Decorator.
*
* @author ahoo wang
*/
@ThreadSafe
public interface Decorator<D> {
/**
* Get decorator actual id generator.
*
* @return actual id generator
*/
@Nonnull
D getActual();

@SuppressWarnings({"unchecked", "rawtypes"})
static <D> D getActual(D any) {
if (any instanceof Decorator decorator) {
return getActual((D) decorator.getActual());
}
return any;
}

@SuppressWarnings({"rawtypes"})
static String chain(Object any) {
StringBuilder builder = new StringBuilder();
builder.append(any.getClass().getSimpleName());

while (any instanceof Decorator decorator) {
any = decorator.getActual();
builder.append(" -> ").append(any.getClass().getSimpleName());
}

return builder.toString();
}
}
10 changes: 3 additions & 7 deletions cosid-core/src/main/java/me/ahoo/cosid/IdGeneratorDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @author ahoo wang
*/
@ThreadSafe
public interface IdGeneratorDecorator extends IdGenerator {
public interface IdGeneratorDecorator extends IdGenerator, Decorator<IdGenerator> {
/**
* Get decorator actual id generator.
*
Expand All @@ -18,12 +18,8 @@ public interface IdGeneratorDecorator extends IdGenerator {
@Nonnull
IdGenerator getActual();

@SuppressWarnings("unchecked")
static <T extends IdGenerator> T getActual(IdGenerator idGenerator) {
if (idGenerator instanceof IdGeneratorDecorator) {
return getActual(((IdGeneratorDecorator) idGenerator).getActual());
}
return (T) idGenerator;
static <T extends IdGenerator> T getActual(T idGenerator) {
return Decorator.getActual(idGenerator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,29 @@

package me.ahoo.cosid.converter;

import me.ahoo.cosid.Decorator;
import me.ahoo.cosid.IdConverter;
import me.ahoo.cosid.segment.grouped.GroupedAccessor;

import com.google.common.base.Preconditions;

import javax.annotation.Nonnull;

public class GroupedPrefixIdConverter implements IdConverter {
public class GroupedPrefixIdConverter implements IdConverter, Decorator<IdConverter> {
public static final String DEFAULT_DELIMITER = "-";
private final String delimiter;
private final IdConverter idConverter;
private final IdConverter actual;

public GroupedPrefixIdConverter(String delimiter, IdConverter idConverter) {
public GroupedPrefixIdConverter(String delimiter, IdConverter actual) {
Preconditions.checkNotNull(delimiter, "prefix can not be null!");
this.delimiter = delimiter;
this.idConverter = idConverter;
this.actual = actual;
}

@Nonnull
@Override
public IdConverter getActual() {
return actual;
}

public String getDelimiter() {
Expand All @@ -38,7 +45,7 @@ public String getDelimiter() {
@Nonnull
@Override
public String asString(long id) {
String idStr = idConverter.asString(id);
String idStr = actual.asString(id);
String groupKey = GroupedAccessor.requiredGet().getKey();
if (delimiter.isEmpty()) {
return groupKey + idStr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package me.ahoo.cosid.converter;

import me.ahoo.cosid.Decorator;
import me.ahoo.cosid.IdConverter;

import com.google.common.base.Preconditions;
Expand All @@ -24,15 +25,21 @@
*
* @author ahoo wang
*/
public class PrefixIdConverter implements IdConverter {
public class PrefixIdConverter implements IdConverter, Decorator<IdConverter> {

private final String prefix;
private final IdConverter idConverter;
private final IdConverter actual;

public PrefixIdConverter(String prefix, IdConverter idConverter) {
public PrefixIdConverter(String prefix, IdConverter actual) {
Preconditions.checkNotNull(prefix, "prefix can not be null!");
this.prefix = prefix;
this.idConverter = idConverter;
this.actual = actual;
}

@Nonnull
@Override
public IdConverter getActual() {
return actual;
}

public String getPrefix() {
Expand All @@ -42,7 +49,7 @@ public String getPrefix() {
@Nonnull
@Override
public String asString(long id) {
String idStr = idConverter.asString(id);
String idStr = actual.asString(id);
if (prefix.isEmpty()) {
return idStr;
}
Expand All @@ -52,6 +59,6 @@ public String asString(long id) {
@Override
public long asLong(@Nonnull String idString) {
String idStr = idString.substring(prefix.length());
return idConverter.asLong(idStr);
return actual.asLong(idStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package me.ahoo.cosid.converter;


import me.ahoo.cosid.Decorator;
import me.ahoo.cosid.IdConverter;

import com.google.common.base.Preconditions;
Expand All @@ -25,14 +26,20 @@
*
* @author ahoo wang
*/
public class SuffixIdConverter implements IdConverter {
public class SuffixIdConverter implements IdConverter, Decorator<IdConverter> {
private final String suffix;
private final IdConverter idConverter;
private final IdConverter actual;

public SuffixIdConverter(String suffix, IdConverter idConverter) {
public SuffixIdConverter(String suffix, IdConverter actual) {
Preconditions.checkNotNull(suffix, "suffix can not be null!");
this.suffix = suffix;
this.idConverter = idConverter;
this.actual = actual;
}

@Nonnull
@Override
public IdConverter getActual() {
return actual;
}

public String getSuffix() {
Expand All @@ -42,7 +49,7 @@ public String getSuffix() {
@Nonnull
@Override
public String asString(long id) {
String idStr = idConverter.asString(id);
String idStr = actual.asString(id);
if (suffix.isEmpty()) {
return idStr;
}
Expand All @@ -52,6 +59,6 @@ public String asString(long id) {
@Override
public long asLong(@Nonnull String idString) {
String idStr = idString.substring(0, idString.length() - suffix.length());
return idConverter.asLong(idStr);
return actual.asLong(idStr);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -83,6 +85,11 @@ public void clear() {
nameMapIdGen.clear();
}

@Override
public Set<Map.Entry<String, IdGenerator>> entries() {
return nameMapIdGen.entrySet();
}

@Override
public Collection<IdGenerator> getAll() {
return nameMapIdGen.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
* {@link IdGenerator} container.
Expand All @@ -28,6 +30,7 @@
*/
@ThreadSafe
public interface IdGeneratorProvider {

/**
* the key of shared ID generator.
*/
Expand Down Expand Up @@ -88,6 +91,8 @@ default IdGenerator getRequired(String name) {
*/
void clear();

Set<Map.Entry<String, IdGenerator>> entries();

/**
* get all ID generator.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class LazyIdGenerator implements IdGeneratorDecorator {

private final String generatorName;

private IdGenerator lazyIdGen;
private volatile IdGenerator lazyIdGen;

private final IdGeneratorProvider idGeneratorProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public DefaultSegmentId(long idSegmentTtl, IdSegmentDistributor maxIdDistributor
this.maxIdDistributor = maxIdDistributor;
}

@Override
public IdSegment current() {
return segment;
}

@Override
public long generate() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public SegmentChainId(long idSegmentTtl, int safeDistance, IdSegmentDistributor
prefetchWorkerExecutorService.submit(prefetchJob);
}

@Override
public IdSegment current() {
return headChain;
}

public IdSegmentChain getHead() {
return headChain;
}
Expand Down
2 changes: 2 additions & 0 deletions cosid-core/src/main/java/me/ahoo/cosid/segment/SegmentId.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
*/
public interface SegmentId extends IdGenerator {
int ONE_STEP = 1;

IdSegment current();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
* @author ahoo wang
*/
public class StringSegmentId extends StringIdGeneratorDecorator implements SegmentId {

private final SegmentId actualSegmentId;

public StringSegmentId(SegmentId actual, IdConverter idConverter) {
super(actual, idConverter);
this.actualSegmentId = actual;
}

@Override
public IdSegment current() {
return actualSegmentId.current();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public static GroupedKey get() {
public static GroupedKey requiredGet() {
return Objects.requireNonNull(get(), "The current thread has not set the GroupedKey.");
}

public static void clear() {
CURRENT.remove();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ default boolean isSafeJavascript() {

int getMachineId();


static long defaultSequenceResetThreshold(int sequenceBit) {
return ~(-1L << (sequenceBit - 1));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed 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 me.ahoo.cosid.stat;

record CosIdGeneratorStat(String kind,
String converterKind,
int machineId,
long lastTimestamp) implements Stat {

}
27 changes: 27 additions & 0 deletions cosid-core/src/main/java/me/ahoo/cosid/stat/SegmentIdStat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright [2021-present] [ahoo wang <ahoowang@qq.com> (https://github.com/Ahoo-Wang)].
* Licensed 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 me.ahoo.cosid.stat;

public record SegmentIdStat(String kind,
String converterKind,
long fetchTime,
long maxId,
long offset,
long sequence,
long step,
boolean isExpired,
boolean isOverflow,
boolean isAvailable
) implements Stat {
}

0 comments on commit 0197f86

Please sign in to comment.