From 0bc8ce0f8e2257e6bbab1358e1476156d3180897 Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Fri, 11 Sep 2026 16:15:22 +0300 Subject: [PATCH 1/2] Adopt SPDX license headers in newly added files --- .../server/service/mail/EdgeMailRequest.java | 17 ++--------------- .../notification/EdgeNotificationRequest.java | 17 ++--------------- .../server/service/sms/EdgeSmsRequest.java | 17 ++--------------- .../server/edge/SendEmailEdgeTest.java | 17 ++--------------- .../server/edge/SendNotificationEdgeTest.java | 17 ++--------------- .../server/edge/SendSmsEdgeTest.java | 17 ++--------------- .../AdminSettingsEdgeProcessorTest.java | 17 ++--------------- 7 files changed, 14 insertions(+), 105 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java b/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java index 733f824b0e..2f2c020ab4 100644 --- a/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java +++ b/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.service.mail; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java b/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java index b135c574e8..50a06aad49 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java +++ b/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.service.notification; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java b/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java index 649b8a5a85..c3696b21be 100644 --- a/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java +++ b/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.service.sms; import com.fasterxml.jackson.annotation.JsonInclude; diff --git a/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java index edab6722ac..d8c35b1676 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.edge; import com.fasterxml.jackson.databind.JsonNode; diff --git a/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java index 362b7a6403..8787bcde19 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.edge; import org.junit.Test; diff --git a/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java index 190a973481..408bbfc43d 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java +++ b/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.edge; import org.junit.Test; diff --git a/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java b/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java index e3453ea618..9a9d0a9ca8 100644 --- a/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java +++ b/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java @@ -1,18 +1,5 @@ -/** - * Copyright © 2016-2026 The Thingsboard Authors - * - * 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. - */ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 package org.thingsboard.server.service.edge.rpc.processor.settings; import com.fasterxml.jackson.databind.JsonNode; From 0ea5f6a48f7ca13e62bd7bc981fad9430f97205b Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Fri, 11 Sep 2026 16:59:48 +0300 Subject: [PATCH 2/2] Extract one-time admin settings cleanup into AdminSettingsCleanupUtils --- .../service/edge/rpc/EdgeGrpcSession.java | 59 +----------- .../rpc/utils/AdminSettingsCleanupUtils.java | 94 +++++++++++++++++++ 2 files changed, 98 insertions(+), 55 deletions(-) create mode 100644 application/src/main/java/org/thingsboard/server/service/edge/rpc/utils/AdminSettingsCleanupUtils.java diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java index 71fdca3e06..e9b3a09734 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java @@ -24,7 +24,6 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributesSaveResult; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; -import org.thingsboard.server.common.data.kv.BooleanDataEntry; import org.thingsboard.server.common.data.kv.LongDataEntry; import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.limit.LimitedApi; @@ -79,6 +78,7 @@ import org.thingsboard.server.service.edge.EdgeContextComponent; import org.thingsboard.server.service.edge.EdgeMsgConstructorUtils; import org.thingsboard.server.service.edge.rpc.fetch.EdgeEventFetcher; import org.thingsboard.server.service.edge.rpc.fetch.GeneralEdgeEventFetcher; +import org.thingsboard.server.service.edge.rpc.utils.AdminSettingsCleanupUtils; import org.thingsboard.server.service.edge.rpc.utils.EdgeVersionUtils; import java.io.Closeable; @@ -100,7 +100,6 @@ public abstract class EdgeGrpcSession implements Closeable { private static final String QUEUE_START_TS_ATTR_KEY = "queueStartTs"; private static final String QUEUE_START_SEQ_ID_ATTR_KEY = "queueStartSeqId"; - private static final String ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY = "adminSettingsCleanupCompleted"; private static final int MAX_DOWNLINK_ATTEMPTS = 3; private static final String RATE_LIMIT_REACHED = "Rate limit reached"; @@ -241,64 +240,14 @@ public abstract class EdgeGrpcSession implements Closeable { log.info("[{}][{}][{}] Staring edge sync process", tenantId, edge.getId(), sessionId); syncInProgress = true; interruptGeneralProcessingOnSync(); - sendOneTimeAdminSettingsCleanupIfNeeded(() -> doSync(new EdgeSyncCursor(ctx, edge, fullSync))); + AdminSettingsCleanupUtils.sendOneTimeAdminSettingsCleanupIfNeeded(ctx, edge, edgeVersion, + EdgeVersion.V_4_2_2_4, this::sendDownlinkMsgsPack, + () -> doSync(new EdgeSyncCursor(ctx, edge, fullSync))); } else { log.info("[{}][{}][{}] Sync is already started, skipping starting it now", tenantId, edge.getId(), sessionId); } } - private void sendOneTimeAdminSettingsCleanupIfNeeded(Runnable continuation) { - if (!EdgeVersionUtils.isEdgeVersionOlderThan(edgeVersion, EdgeVersion.V_4_2_2_4)) { - continuation.run(); - return; - } - try { - ListenableFuture> isCleanupCompletedAttrFuture = ctx.getAttributesService() - .find(tenantId, edge.getId(), AttributeScope.SERVER_SCOPE, ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY); - - ListenableFuture cleanupFuture = Futures.transformAsync( - isCleanupCompletedAttrFuture, - attr -> { - boolean alreadyCompleted = attr.isPresent() && attr.flatMap(AttributeKvEntry::getBooleanValue).orElse(false); - if (alreadyCompleted) { - return Futures.immediateFuture(false); - } - List cleanupMsgs = ctx.getAdminSettingsProcessor().convertAdminSettingsCleanupToDownlinks(edge); - if (cleanupMsgs.isEmpty()) { - return Futures.immediateFuture(true); - } - // send the cleanup pack; mark completed only if it was not interrupted, so it retries on next sync otherwise - return Futures.transform(sendDownlinkMsgsPack(cleanupMsgs), - isInterrupted -> !Boolean.TRUE.equals(isInterrupted), ctx.getGrpcCallbackExecutorService()); - }, ctx.getGrpcCallbackExecutorService()); - - Futures.addCallback(cleanupFuture, new FutureCallback<>() { - @Override - public void onSuccess(@Nullable Boolean markCompleted) { - if (Boolean.TRUE.equals(markCompleted)) { - markAdminSettingsCleanupCompleted(); - } - continuation.run(); - } - - @Override - public void onFailure(Throwable t) { - log.error("[{}][{}] Failed to run one-time admin settings cleanup for edge", tenantId, edge.getId(), t); - continuation.run(); - } - }, ctx.getGrpcCallbackExecutorService()); - } catch (Exception e) { - log.error("[{}][{}] Failed to start admin settings cleanup", tenantId, edge.getId(), e); - continuation.run(); - } - } - - private void markAdminSettingsCleanupCompleted() { - AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry( - new BooleanDataEntry(ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY, true), System.currentTimeMillis()); - ctx.getAttributesService().save(tenantId, edge.getId(), AttributeScope.SERVER_SCOPE, attributeKvEntry); - } - private void doSync(EdgeSyncCursor cursor) { if (cursor.hasNext()) { EdgeEventFetcher next = cursor.getNext(); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/utils/AdminSettingsCleanupUtils.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/utils/AdminSettingsCleanupUtils.java new file mode 100644 index 0000000000..cd0fd2ec35 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/utils/AdminSettingsCleanupUtils.java @@ -0,0 +1,94 @@ +// SPDX-FileCopyrightText: Copyright The Thingsboard Authors +// SPDX-License-Identifier: Apache-2.0 +package org.thingsboard.server.service.edge.rpc.utils; + +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.thingsboard.server.common.data.AttributeScope; +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; +import org.thingsboard.server.common.data.kv.BooleanDataEntry; +import org.thingsboard.server.gen.edge.v1.DownlinkMsg; +import org.thingsboard.server.gen.edge.v1.EdgeVersion; +import org.thingsboard.server.service.edge.EdgeContextComponent; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; + +@Slf4j +public final class AdminSettingsCleanupUtils { + + private static final String ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY = "adminSettingsCleanupCompleted"; + + /** + * Sends the one-time admin settings cleanup pack to edges that were provisioned before admin settings sync was + * removed, then runs {@code continuation}. Edges on {@code cleanupUpToVersion} or newer never received the + * settings, so they are skipped. The completion flag is stored as a server attribute on the edge, so the cleanup + * is sent once; if the pack is interrupted the flag is not set and the cleanup is retried on the next sync. + * + * @param downlinkMsgsPackSender sends a downlink pack, returning whether the send was interrupted + * @param continuation always invoked, whether or not the cleanup ran or failed + */ + public static void sendOneTimeAdminSettingsCleanupIfNeeded(EdgeContextComponent ctx, Edge edge, EdgeVersion edgeVersion, + EdgeVersion cleanupUpToVersion, + Function, ListenableFuture> downlinkMsgsPackSender, + Runnable continuation) { + if (!EdgeVersionUtils.isEdgeVersionOlderThan(edgeVersion, cleanupUpToVersion)) { + continuation.run(); + return; + } + TenantId tenantId = edge.getTenantId(); + try { + ListenableFuture> isCleanupCompletedAttrFuture = ctx.getAttributesService() + .find(tenantId, edge.getId(), AttributeScope.SERVER_SCOPE, ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY); + + ListenableFuture cleanupFuture = Futures.transformAsync( + isCleanupCompletedAttrFuture, + attr -> { + boolean alreadyCompleted = attr.isPresent() && attr.flatMap(AttributeKvEntry::getBooleanValue).orElse(false); + if (alreadyCompleted) { + return Futures.immediateFuture(false); + } + List cleanupMsgs = ctx.getAdminSettingsProcessor().convertAdminSettingsCleanupToDownlinks(edge); + if (cleanupMsgs.isEmpty()) { + return Futures.immediateFuture(true); + } + // send the cleanup pack; mark completed only if it was not interrupted, so it retries on next sync otherwise + return Futures.transform(downlinkMsgsPackSender.apply(cleanupMsgs), + isInterrupted -> !Boolean.TRUE.equals(isInterrupted), ctx.getGrpcCallbackExecutorService()); + }, ctx.getGrpcCallbackExecutorService()); + + Futures.addCallback(cleanupFuture, new FutureCallback<>() { + @Override + public void onSuccess(@Nullable Boolean markCompleted) { + if (Boolean.TRUE.equals(markCompleted)) { + markAdminSettingsCleanupCompleted(ctx, edge); + } + continuation.run(); + } + + @Override + public void onFailure(Throwable t) { + log.error("[{}][{}] Failed to run one-time admin settings cleanup for edge", tenantId, edge.getId(), t); + continuation.run(); + } + }, ctx.getGrpcCallbackExecutorService()); + } catch (Exception e) { + log.error("[{}][{}] Failed to start admin settings cleanup", tenantId, edge.getId(), e); + continuation.run(); + } + } + + private static void markAdminSettingsCleanupCompleted(EdgeContextComponent ctx, Edge edge) { + AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry( + new BooleanDataEntry(ADMIN_SETTINGS_CLEANUP_COMPLETED_ATTR_KEY, true), System.currentTimeMillis()); + ctx.getAttributesService().save(edge.getTenantId(), edge.getId(), AttributeScope.SERVER_SCOPE, attributeKvEntry); + } + +}