Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to create service with labelSelector. #4436

Closed
Xezeloh opened this issue Dec 9, 2020 · 2 comments
Closed

Fail to create service with labelSelector. #4436

Xezeloh opened this issue Dec 9, 2020 · 2 comments
Labels
area/Naming kind/bug Category issues or prs related to bug.
Milestone

Comments

@Xezeloh
Copy link

Xezeloh commented Dec 9, 2020

Describe the bug
I tried to create a service with labelSelector but got this:

ErrCode:400, ErrMsg:failed to req API:/api//nacos/v1/ns/service after all servers([127.0.0.1:8848]) tried: caused: specified service already exists, serviceName : DEFAULT_GROUP@@cross-site-server;
	at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:490)
	at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:395)
	at com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI(NamingProxy.java:391)
	at com.alibaba.nacos.client.naming.net.NamingProxy.createService(NamingProxy.java:276)
	at com.alibaba.nacos.client.naming.NacosNamingMaintainService.createService(NacosNamingMaintainService.java:133)
	at com.alibaba.nacos.client.naming.NacosNamingMaintainService.createService(NacosNamingMaintainService.java:128)
	at com.alibaba.nacos.spring.factory.DelegatingNamingMaintainService.createService(DelegatingNamingMaintainService.java:89)
...

My code is as below:

@SpringBootApplication
public class CrossSiteServerApplication {

	@NacosInjected
	private NamingMaintainService namingMaintainService;

	@NacosInjected
	private NamingService namingService;

	@Value("${spring.application.name}")
	private String applicationName;

	@Value("${service.provider.ip}")
	private String ip;

	@Value("${service.provider.port}")
	private Integer port;

	@PostConstruct
	private void registerInstance() throws NacosException {
		System.out.println(namingMaintainService);

		Service service = new Service();
		service.setAppName(applicationName);
		service.setGroupName(Constants.DEFAULT_GROUP);
		try {
			namingMaintainService.createService(
					applicationName,
					Constants.DEFAULT_GROUP,
					0,
					"CONSUMER.label.site = PROVIDER.label.site"
			);
		} catch (NacosException e) {
			e.printStackTrace();
			return;
		}

		try {
			namingService.registerInstance(applicationName, ip, port);
		} catch (NacosException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		SpringApplication.run(CrossSiteServerApplication.class, args);
	}
}

Expected behavior
Service created successfully.

Acutally behavior
Fail to create the service.

How to Reproduce
Steps to reproduce the behavior:

  1. Run the code.
  2. See the error.

Desktop (please complete the following information):

  • OS: Windows 10
  • Version: nacos-server 1.3.2
  • Module: naming
  • SDK: nacos-discovery-spring-boot-starter

Additional context
FYI, here's another way to see a related error on the console:

  1. Click the “Create Service” button.
  2. Type "Dragonborn" in "Service Name" box.
  3. Type "0" in "Protect Threshold" box.
  4. Choose “Label” in the drop-down box "Type".
  5. Type "CONSUMER.label.site = PROVIDER.label.site" in the "Selector" box.
  6. Click the "OK" button.
  7. See the error "caused: parsed expression failed!;"

I try to locate the error and find this:

int index = 0;
index = checkInnerSyntax(elements, index);
if (index == -1) {
throw new NacosException(NacosException.INVALID_PARAM, "parse expression failed!");
}

private static int checkInnerSyntax(List<String> elements, int start) {
int index = start;
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return -1;
}
if (!elements.get(index).startsWith(CONSUMER_PREFIX)) {
return -1;
}
index = skipEmpty(elements, index);
if (index >= elements.size()) {
return -1;
}
if (!SUPPORTED_INNER_CONNCETORS.contains(elements.get(index++))) {
return -1;
}

第一段代码在172行调用了一次checkInnerSyntax方法,并且给形参start赋了0值。而在checkInnerSyntax方法中,第265行,这里用了一个index++来作为索引。结合前后代码,我推测这里或许是想表达“确认下一个元素是否在SUPPORTED_INNER_CONNCETORS”中这样一个逻辑,但却错误地使用了index++,导致了错误的出现。

@horizonzy
Copy link
Collaborator

I will trace it.

@horizonzy
Copy link
Collaborator

It's really a bug, I will fix it.

KomachiSion pushed a commit that referenced this issue Dec 14, 2020
* fix parse label expression error problem

* ignore VariableDeclarationUsageDistance checkstyle

* fix check style
@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. and removed kind/research labels Dec 14, 2020
@KomachiSion KomachiSion added this to the 1.4.1 milestone Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Naming kind/bug Category issues or prs related to bug.
Projects
None yet
Development

No branches or pull requests

3 participants