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

fix(session): killing session may fail due to a wrong regex #4169

Merged
merged 4 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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 com.oceanbase.odc.common.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.validation.constraints.NotNull;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
* @Author: Lebie
* @Date: 2025/1/15 13:47
* @Description: []
*/
@Slf4j
public class HostUtils {
// valid expression examples: 0.0.0.0:8888, 127.1:8888, etc
private static final String SERVER_REGEX = "(?<ip>([0-9]{1,3}\\.){1,3}([0-9]{1,3})):(?<port>[0-9]{1,5})";
private static final Pattern SERVER_PATTERN = Pattern.compile(SERVER_REGEX);


@NotNull
public static ServerAddress extractServerAddress(String ipAndPort) {
String trimmed = StringUtils.trim(ipAndPort);
if (StringUtils.isBlank(trimmed)) {
log.info("unable to extract server address, text is empty");
throw new IllegalArgumentException("Empty server address!");
}
Matcher matcher = SERVER_PATTERN.matcher(trimmed);
MarkPotato777 marked this conversation as resolved.
Show resolved Hide resolved
if (!matcher.matches()) {
log.info("unable to extract server address, does not match pattern");
throw new IllegalArgumentException("Invalid server address!");
}
String ipAddress = matcher.group("ip");
String port = matcher.group("port");
if (StringUtils.isEmpty(ipAddress) || StringUtils.isEmpty(port)) {
log.info("unable to extract server address, ipAddress={}, port={}", ipAddress, port);
throw new IllegalArgumentException("Invalid server address!");
}
return new ServerAddress(ipAddress, port);
}



@Data
public static class ServerAddress {
String ipAddress;
String port;

public ServerAddress(String ipAddress, String port) {
this.ipAddress = ipAddress;
this.port = port;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2023 OceanBase.
*
* 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 com.oceanbase.odc.common.util;

import org.junit.Test;

import com.oceanbase.odc.common.util.HostUtils.ServerAddress;

import junit.framework.Assert;

public class HostUtilsTest {
@Test
public void testExtractServerAddress_ValidExpression() {
String ipAndPort = "1.1.1.1:1234";
ServerAddress actual = HostUtils.extractServerAddress(ipAndPort);
Assert.assertEquals("1.1.1.1", actual.getIpAddress());
Assert.assertEquals("1234", actual.getPort());
}

@Test(expected = IllegalArgumentException.class)
public void testExtractServerAddress_InvalidExpression1() {
String ipAndPort = "1.1.1.1.1:1234";
HostUtils.extractServerAddress(ipAndPort);
}

@Test(expected = IllegalArgumentException.class)
public void testExtractServerAddress_InvalidExpression2() {
String ipAndPort = "1.1.1?1:1234";
HostUtils.extractServerAddress(ipAndPort);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,17 @@
import java.util.concurrent.TimeoutException;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.SetUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.MoreObjects;
import com.google.common.collect.Lists;
import com.oceanbase.odc.common.util.HostUtils;
import com.oceanbase.odc.common.util.HostUtils.ServerAddress;
import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.common.util.VersionUtils;
import com.oceanbase.odc.core.authority.util.SkipAuthorize;
Expand Down Expand Up @@ -82,9 +80,6 @@
@Slf4j
public class DefaultDBSessionManage implements DBSessionManageFacade {

private static final String SERVER_REGEX = ".*(?<ip>([0-9]{1,3}.){1,3}([0-9]{1,3})):"
+ "(?<port>[0-9]{1,5}).*";
private static final Pattern SERVER_PATTERN = Pattern.compile(SERVER_REGEX);
private static final ConnectionMapper CONNECTION_MAPPER = ConnectionMapper.INSTANCE;
private static final String GLOBAL_CLIENT_SESSION_OB_PROXY_VERSION_NUMBER = "4.2.3";
private static final String GLOBAL_CLIENT_SESSION_OB_VERSION_NUMBER = "4.2.5";
Expand Down Expand Up @@ -237,7 +232,7 @@ private List<JdbcGeneralResult> additionalKillIfNecessary(ConnectionSession conn
Map<String, ServerAddress> sessionId2SvrAddr =
getSessionList(connectionSession, s -> s.getSvrIp() != null)
.stream().collect(Collectors.toMap(OdcDBSession::getSessionId,
s -> extractServerAddress(MoreObjects.firstNonNull(s.getSvrIp(), ""))));
s -> HostUtils.extractServerAddress(MoreObjects.firstNonNull(s.getSvrIp(), ""))));
Map<String, String> sqlId2SessionId = sqlTupleSessionIds.stream().collect(
Collectors.toMap(s -> s.getSqlTuple().getSqlId(), SqlTupleSessionId::getSessionId));

Expand Down Expand Up @@ -424,28 +419,6 @@ private void directLinkServerAndExecute(String sql, ConnectionSession session, S
}
}

// extract text(query from the dictionary) to server address(ip, port)
// the text is expected be like 0.0.0.0:8888
@NotNull
private ServerAddress extractServerAddress(String text) {
String trimmed = StringUtils.trim(text);
if (StringUtils.isBlank(trimmed)) {
log.info("unable to extract server address, text is empty");
throw new IllegalStateException("Empty server address!");
}
Matcher matcher = SERVER_PATTERN.matcher(trimmed);
if (!matcher.matches()) {
log.info("unable to extract server address, does not match pattern");
throw new IllegalStateException("Invalid server address!");
}
String ipAddress = matcher.group("ip");
String port = matcher.group("port");
if (StringUtils.isEmpty(ipAddress) || StringUtils.isEmpty(port)) {
log.info("unable to extract server address, ipAddress={}, port={}", ipAddress, port);
throw new IllegalStateException("Invalid server address!");
}
return new ServerAddress(ipAddress, port);
}

private CompletableFuture<Void> doKillAllSessions(List<OdcDBSession> list, ConnectionSession connectionSession,
Executor executor) {
Expand Down Expand Up @@ -477,17 +450,6 @@ private <T> void waitingForResult(Supplier<CompletableFuture<T>> completableFutu
}
}

@Data
static class ServerAddress {
String ipAddress;
String port;

public ServerAddress(String ipAddress, String port) {
this.ipAddress = ipAddress;
this.port = port;
}
}

@AllArgsConstructor
@Data
@NoArgsConstructor
Expand Down
Loading