Skip to content

fix(manager): set instance to 'unknow' for dynamic monitors on modification - #4101

Open
pentium100 wants to merge 33 commits into
apache:masterfrom
pentium100:fix-modify-monitor-instance-bug
Open

fix(manager): set instance to 'unknow' for dynamic monitors on modification#4101
pentium100 wants to merge 33 commits into
apache:masterfrom
pentium100:fix-modify-monitor-instance-bug

Conversation

@pentium100

@pentium100 pentium100 commented Mar 29, 2026

Copy link
Copy Markdown
Contributor

What's changed?

Normalize monitor instances when adding or modifying monitors.

  • dynamic service-discovery monitors consistently use unknown, preventing a
    null instance from reaching immutable metadata construction
  • static monitor edits replace or remove an explicit old port according to the
    current port parameter
  • raw IPv6 hosts are never mistaken for host:port; a configured port produces
    the unambiguous bracketed form ([2001:db8::1]:443)
  • bracketed IPv6, IPv4, and domain instances remain stable across repeated edits
  • the branch is merged with current master and the earlier formatting-only
    churn has been removed

Regression proof

The latest-master baseline reproduced the service-discovery failure with
expected: <unknown> but was: <memory>. The corrected implementation passes:

  • all 46 MonitorServiceTest tests
  • 11 focused modify/instance tests, including domain, IPv4, raw IPv6, bracketed
    IPv6, port addition, replacement, and removal
  • Maven Checkstyle and git diff apache/master --check

OCR delegation resolved the Java correctness, boundary, performance, and thread
safety rules. The test diff was manually reviewed because the deterministic
selector excludes test paths; no credible high or medium finding remains.

AI assistance: used for conflict resolution, regression coverage, and test
iteration.
Human validation: reproduced the old failure and ran the complete
MonitorServiceTest suite on Java 25.
Risk notes: an unbracketed IPv6 literal is always treated as a host; ports are
represented with brackets to avoid an ambiguous final numeric segment.

…cation

In the `modifyMonitor` method, the `instance` field for dynamically discovered monitors (where `isStatic` is false) was not being explicitly set to "unknow". This could lead to incorrect instance values or unexpected behavior when updating service discovery monitors.

This commit aligns the behavior of `modifyMonitor` with `addMonitor` by explicitly setting `monitor.setInstance("unknow")` for non-static scrape types before processing the instance and port fields.
…cation

In the `modifyMonitor` method, the `instance` field for dynamically discovered monitors (where `isStatic` is false) was not being explicitly set to "unknow". This could lead to incorrect instance values or unexpected behavior when updating service discovery monitors.

This commit aligns the behavior of `modifyMonitor` with `addMonitor` by explicitly setting `monitor.setInstance("unknow")` for non-static scrape types before processing the instance and port fields.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an exception when modifying service-discovery (non-static scrape) monitors by ensuring monitor.instance is set to a non-null placeholder before building job metadata (aligning modifyMonitor behavior with addMonitor). This addresses issue #4100 where Map.of(...) would throw due to a null instance.

Changes:

  • In modifyMonitor, compute isStatic earlier and set monitor.instance to a placeholder value for non-static scrapes.
  • Prevent null instance from reaching Map.of(...) during job metadata construction for service discovery monitors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 391 to 403
@@ -396,8 +402,6 @@ public void modifyMonitor(Monitor monitor, List<Param> params, String collector,
}
monitor.setInstance(instance);

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifyMonitor appends portWithMark whenever instance is non-null, even if instance already includes a ":port" suffix. This can produce duplicated ports (e.g., host:9200:9200) for API clients/imports that send monitor.instance with port included. Align this logic with addMonitor by checking IpDomainUtil.isHasPortWithMark(instance) before concatenating the port.

Copilot uses AI. Check for mistakes.
@yuluo-yx

Copy link
Copy Markdown
Member

hi @pentium100 pls see the copilot review comment.

When the monitor type is service discovery and the instance is not empty,
the instance value should not be overwritten. This commit fixes this issue.
Additionally, it corrects the spelling of 'unknow' to 'unknown' and prevents
appending the port if the instance already contains it.
zqr10159
zqr10159 previously approved these changes Mar 30, 2026
zqr10159
zqr10159 previously approved these changes Mar 31, 2026
@zqr10159
zqr10159 requested a review from Duansg March 31, 2026 08:06

@Duansg Duansg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, please update the regression test cases for this change.

@pentium100
pentium100 requested a review from Duansg April 6, 2026 12:19
Removed the unnecessary check for port existence in the `instance`
string, as `instance` will not contain port information in this context.
@pentium100
pentium100 requested a review from Duansg April 11, 2026 11:39
@Duansg

Duansg commented Apr 18, 2026

Copy link
Copy Markdown
Member

org.apache.hertzbeat.manager.service.MonitorServiceTest#modifyMonitor please review this method, as it may cause CI issues later on.

@pentium100
pentium100 requested a review from Duansg April 25, 2026 09:11

@Duansg Duansg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pentium100 Hi, Please continue reviewing the code. If you encounter any issues, please refer to the Code Style and Quality Guide first.

* Added logic in `MonitorServiceImpl.java` to check if the `instance` string contains a port, and if so, strips the port out to only retain the IP or domain.
@pentium100
pentium100 requested a review from Duansg May 5, 2026 13:47
Duansg and others added 6 commits May 6, 2026 15:21
…r build

The Docker build was failing because /var/run/sshd already exists in the
base image. Using mkdir -p allows the command to succeed if the directory
already exists, preventing exit code 1 errors.

Fixes apache#4101

@zqr10159 zqr10159 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The null-instance fix for service-discovery monitors is directionally correct, but the new port-removal path corrupts valid IPv6 instances. IpDomainUtil.isHasPortWithMark("2001:db8::1") treats the final numeric segment as a port, so lines 394-395 truncate an unbracketed IPv6 address to 2001:db8: even when no port parameter is supplied. Please use an IPv6-safe host/port representation or parser and add focused modify-monitor cases for IPv4/domain hosts plus bracketed and unbracketed IPv6, both with and without ports. The large unrelated indentation churn should also be removed before merge.

@zqr10159 zqr10159 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed after the latest-master merge. The previous IPv6 truncation and formatting churn are gone; dynamic monitor identity is non-null, and explicit port replacement/removal is covered for domains, IPv4, raw IPv6, and bracketed IPv6. The complete MonitorServiceTest suite passes locally (46 tests) with Checkstyle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants