Skip to content

Commit 392a2f2

Browse files
authored
Merge branch 'dev' into dev-datavines
2 parents 8843539 + 9d14144 commit 392a2f2

File tree

16 files changed

+339
-232
lines changed

16 files changed

+339
-232
lines changed

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/interceptor/LoginHandlerInterceptor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
9696
}
9797

9898
@Override
99-
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
100-
ModelAndView modelAndView) throws Exception {
99+
public void postHandle(HttpServletRequest request,
100+
HttpServletResponse response,
101+
Object handler,
102+
ModelAndView modelAndView) {
101103
ThreadLocalContext.getTimezoneThreadLocal().remove();
102104

103105
int code = response.getStatus();

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,14 @@ public User getAuthUser(HttpServletRequest request) {
119119
sessionId = cookie.getValue();
120120
}
121121
}
122-
Session session = sessionService.getSession(sessionId);
122+
final Session session = sessionService.getSession(sessionId);
123123
if (session == null) {
124124
return null;
125125
}
126+
if (sessionService.isSessionExpire(session)) {
127+
sessionService.expireSession(session.getUserId());
128+
return null;
129+
}
126130
// get user object from session
127131
return userService.queryUser(session.getUserId());
128132
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/pwd/PasswordAuthenticator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.apache.dolphinscheduler.api.security.impl.AbstractAuthenticator;
2121
import org.apache.dolphinscheduler.dao.entity.User;
2222

23+
import lombok.NonNull;
24+
2325
public class PasswordAuthenticator extends AbstractAuthenticator {
2426

2527
@Override
26-
public User login(String userName, String password) {
28+
public User login(@NonNull String userName, String password) {
2729
return userService.queryUser(userName, password);
2830
}
2931
}

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SessionServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void expireSession(Integer userId) {
9494

9595
@Override
9696
public boolean isSessionExpire(Session session) {
97-
return System.currentTimeMillis() - session.getLastLoginTime().getTime() <= Constants.SESSION_TIME_OUT * 1000;
97+
return System.currentTimeMillis() - session.getLastLoginTime().getTime() >= Constants.SESSION_TIME_OUT * 1000;
9898
}
9999

100100
}

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
import org.apache.dolphinscheduler.api.utils.Result;
2828
import org.apache.dolphinscheduler.common.constants.Constants;
2929
import org.apache.dolphinscheduler.common.utils.JSONUtils;
30+
import org.apache.dolphinscheduler.dao.entity.Session;
31+
import org.apache.dolphinscheduler.dao.repository.SessionDao;
3032

33+
import org.apache.http.HttpStatus;
34+
35+
import java.util.Date;
3136
import java.util.Map;
3237

3338
import javax.servlet.http.Cookie;
@@ -36,6 +41,7 @@
3641
import org.junit.jupiter.api.Test;
3742
import org.slf4j.Logger;
3843
import org.slf4j.LoggerFactory;
44+
import org.springframework.beans.factory.annotation.Autowired;
3945
import org.springframework.http.MediaType;
4046
import org.springframework.mock.web.MockHttpServletResponse;
4147
import org.springframework.test.web.servlet.MvcResult;
@@ -49,6 +55,9 @@ public class LoginControllerTest extends AbstractControllerTest {
4955

5056
private static final Logger logger = LoggerFactory.getLogger(LoginControllerTest.class);
5157

58+
@Autowired
59+
private SessionDao sessionDao;
60+
5261
@Test
5362
public void testLogin() throws Exception {
5463
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
@@ -85,6 +94,18 @@ public void testSignOut() throws Exception {
8594
logger.info(mvcResult.getResponse().getContentAsString());
8695
}
8796

97+
@Test
98+
void testSignOutWithExpireSession() throws Exception {
99+
final Session session = sessionDao.queryById(sessionId);
100+
session.setLastLoginTime(new Date(System.currentTimeMillis() - Constants.SESSION_TIME_OUT * 1000 - 1));
101+
sessionDao.updateById(session);
102+
103+
mockMvc.perform(post("/signOut")
104+
.header("sessionId", sessionId))
105+
.andExpect(status().is(HttpStatus.SC_UNAUTHORIZED))
106+
.andReturn();
107+
}
108+
88109
@Test
89110
void testClearCookie() throws Exception {
90111
MvcResult mvcResult = mockMvc.perform(delete("/cookies")

dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/cluster/AbstractClusterSubscribeListener.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public void notify(Event event) {
3030
try {
3131
// make sure the event is processed in order
3232
synchronized (this) {
33-
Event.Type type = event.type();
34-
T server = parseServerFromHeartbeat(event.data());
33+
Event.Type type = event.getType();
34+
T server = parseServerFromHeartbeat(event.getEventData());
3535
if (server == null) {
3636
log.error("Unknown cluster change event: {}", event);
3737
return;
@@ -58,6 +58,11 @@ public void notify(Event event) {
5858
}
5959
}
6060

61+
@Override
62+
public SubscribeScope getSubscribeScope() {
63+
return SubscribeScope.CHILDREN_ONLY;
64+
}
65+
6166
abstract T parseServerFromHeartbeat(String serverHeartBeatJson);
6267

6368
public abstract void onServerAdded(T serverHeartBeat);

dolphinscheduler-registry/dolphinscheduler-registry-api/src/main/java/org/apache/dolphinscheduler/registry/api/Event.java

Lines changed: 13 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -17,115 +17,30 @@
1717

1818
package org.apache.dolphinscheduler.registry.api;
1919

20+
import lombok.AllArgsConstructor;
21+
import lombok.Builder;
22+
import lombok.Getter;
23+
import lombok.ToString;
24+
25+
@Getter
26+
@ToString
27+
@Builder
28+
@AllArgsConstructor
2029
public class Event {
2130

22-
// The prefix which is watched
23-
private String key;
31+
// The path which is watched
32+
private final String watchedPath;
2433
// The full path where the event was generated
25-
private String path;
34+
private final String eventPath;
2635
// The value corresponding to the path
27-
private String data;
36+
private final String eventData;
2837
// The event type {ADD, REMOVE, UPDATE}
2938
private Type type;
3039

31-
public Event(String key, String path, String data, Type type) {
32-
this.key = key;
33-
this.path = path;
34-
this.data = data;
35-
this.type = type;
36-
}
37-
38-
public Event() {
39-
}
40-
41-
public static EventBuilder builder() {
42-
return new EventBuilder();
43-
}
44-
45-
public String key() {
46-
return this.key;
47-
}
48-
49-
public String path() {
50-
return this.path;
51-
}
52-
53-
public String data() {
54-
return this.data;
55-
}
56-
57-
public Type type() {
58-
return this.type;
59-
}
60-
61-
public Event key(String key) {
62-
this.key = key;
63-
return this;
64-
}
65-
66-
public Event path(String path) {
67-
this.path = path;
68-
return this;
69-
}
70-
71-
public Event data(String data) {
72-
this.data = data;
73-
return this;
74-
}
75-
76-
public Event type(Type type) {
77-
this.type = type;
78-
return this;
79-
}
80-
81-
public String toString() {
82-
return "Event(key=" + this.key() + ", path=" + this.path() + ", data=" + this.data() + ", type=" + this.type()
83-
+ ")";
84-
}
85-
8640
public enum Type {
8741
ADD,
8842
REMOVE,
8943
UPDATE
9044
}
9145

92-
public static class EventBuilder {
93-
94-
private String key;
95-
private String path;
96-
private String data;
97-
private Type type;
98-
99-
EventBuilder() {
100-
}
101-
102-
public EventBuilder key(String key) {
103-
this.key = key;
104-
return this;
105-
}
106-
107-
public EventBuilder path(String path) {
108-
this.path = path;
109-
return this;
110-
}
111-
112-
public EventBuilder data(String data) {
113-
this.data = data;
114-
return this;
115-
}
116-
117-
public EventBuilder type(Type type) {
118-
this.type = type;
119-
return this;
120-
}
121-
122-
public Event build() {
123-
return new Event(key, path, data, type);
124-
}
125-
126-
public String toString() {
127-
return "Event.EventBuilder(key=" + this.key + ", path=" + this.path + ", data=" + this.data + ", type="
128-
+ this.type + ")";
129-
}
130-
}
13146
}

dolphinscheduler-registry/dolphinscheduler-registry-api/src/main/java/org/apache/dolphinscheduler/registry/api/SubscribeListener.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,23 @@
1919

2020
public interface SubscribeListener {
2121

22-
void notify(Event event);
22+
void notify(final Event event);
23+
24+
SubscribeScope getSubscribeScope();
25+
26+
enum SubscribeScope {
27+
/**
28+
* Only watch the path itself
29+
*/
30+
PATH_ONLY,
31+
/**
32+
* Only watch the children of the path
33+
*/
34+
CHILDREN_ONLY,
35+
/**
36+
* Watch the path and all its children and the parent path
37+
*/
38+
ALL
39+
40+
}
2341
}

dolphinscheduler-registry/dolphinscheduler-registry-api/src/main/java/org/apache/dolphinscheduler/registry/api/ha/AbstractHAServer.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.dolphinscheduler.common.thread.ThreadUtils;
2323
import org.apache.dolphinscheduler.registry.api.Event;
2424
import org.apache.dolphinscheduler.registry.api.Registry;
25+
import org.apache.dolphinscheduler.registry.api.SubscribeListener;
2526

2627
import java.util.List;
2728

@@ -56,16 +57,25 @@ public AbstractHAServer(final Registry registry, final String selectorPath, fina
5657

5758
@Override
5859
public void start() {
59-
registry.subscribe(selectorPath, event -> {
60-
if (Event.Type.REMOVE.equals(event.type())) {
61-
if (serverIdentify.equals(event.data())) {
62-
statusChange(ServerStatus.STAND_BY);
63-
} else {
64-
if (participateElection()) {
65-
statusChange(ServerStatus.ACTIVE);
60+
registry.subscribe(selectorPath, new SubscribeListener() {
61+
62+
@Override
63+
public void notify(Event event) {
64+
if (Event.Type.REMOVE.equals(event.getType())) {
65+
if (serverIdentify.equals(event.getEventData())) {
66+
statusChange(ServerStatus.STAND_BY);
67+
} else {
68+
if (participateElection()) {
69+
statusChange(ServerStatus.ACTIVE);
70+
}
6671
}
6772
}
6873
}
74+
75+
@Override
76+
public SubscribeScope getSubscribeScope() {
77+
return SubscribeScope.PATH_ONLY;
78+
}
6979
});
7080

7181
if (participateElection()) {

0 commit comments

Comments
 (0)