Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
熊鹏飞
/
xxljob220
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
1874cdfd
authored
Mar 12, 2017
by
xueli.xue
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
执行器LFU策略功能实现
parent
177ab8d2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
4 deletions
xxl-job-admin/src/main/java/com/xxl/job/admin/core/route/ExecutorRouter.java
xxl-job-admin/src/main/java/com/xxl/job/admin/core/route/strategy/ExecutorRouteLFU.java
xxl-job-admin/src/main/java/com/xxl/job/admin/core/route/ExecutorRouter.java
View file @
1874cdfd
...
...
@@ -25,7 +25,7 @@ public abstract class ExecutorRouter {
for
(
int
i
=
0
;
i
<
100
;
i
++)
{
String
ret
=
ExecutorRouter
.
route
(
666
,
new
ArrayList
<
String
>(
Arrays
.
asList
(
"127.0.0.1:0000"
,
"127.0.0.1:2222"
,
"127.0.0.1:3333"
)),
ExecutorRouteStrategyEnum
.
LEAST_
REC
ENTLY_USED
.
name
());
String
ret
=
ExecutorRouter
.
route
(
666
,
new
ArrayList
<
String
>(
Arrays
.
asList
(
"127.0.0.1:0000"
,
"127.0.0.1:2222"
,
"127.0.0.1:3333"
)),
ExecutorRouteStrategyEnum
.
LEAST_
FREQU
ENTLY_USED
.
name
());
System
.
out
.
println
(
ret
);
}
...
...
xxl-job-admin/src/main/java/com/xxl/job/admin/core/route/strategy/ExecutorRouteLFU.java
View file @
1874cdfd
...
...
@@ -2,7 +2,8 @@ package com.xxl.job.admin.core.route.strategy;
import
com.xxl.job.admin.core.route.ExecutorRouter
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
/**
* 单个JOB对应的每个执行器,使用频率最低的优先被选举
...
...
@@ -13,12 +14,44 @@ import java.util.ArrayList;
*/
public
class
ExecutorRouteLFU
extends
ExecutorRouter
{
private
static
ConcurrentHashMap
<
Integer
,
HashMap
<
String
,
Integer
>>
jobLfuMap
=
new
ConcurrentHashMap
<
Integer
,
HashMap
<
String
,
Integer
>>();
private
static
long
CACHE_VALID_TIME
=
0
;
@Override
public
String
route
(
int
jobId
,
ArrayList
<
String
>
addressList
)
{
// TODO
// cache clear
if
(
System
.
currentTimeMillis
()
>
CACHE_VALID_TIME
)
{
jobLfuMap
.
clear
();
CACHE_VALID_TIME
=
System
.
currentTimeMillis
()
+
1000
*
60
*
60
*
24
;
}
// lfu item init
HashMap
<
String
,
Integer
>
lfuItemMap
=
jobLfuMap
.
get
(
jobId
);
// Key排序可以用TreeMap+构造入参Compare;Value排序暂时只能通过ArrayList;
if
(
lfuItemMap
==
null
)
{
lfuItemMap
=
new
HashMap
<
String
,
Integer
>();
jobLfuMap
.
put
(
jobId
,
lfuItemMap
);
}
for
(
String
address:
addressList
)
{
if
(!
lfuItemMap
.
containsKey
(
address
))
{
lfuItemMap
.
put
(
address
,
0
);
}
}
// load least userd count address
List
<
Map
.
Entry
<
String
,
Integer
>>
lfuItemList
=
new
ArrayList
<
Map
.
Entry
<
String
,
Integer
>>(
lfuItemMap
.
entrySet
());
Collections
.
sort
(
lfuItemList
,
new
Comparator
<
Map
.
Entry
<
String
,
Integer
>>()
{
@Override
public
int
compare
(
Map
.
Entry
<
String
,
Integer
>
o1
,
Map
.
Entry
<
String
,
Integer
>
o2
)
{
return
o1
.
getValue
().
compareTo
(
o2
.
getValue
());
}
});
Map
.
Entry
<
String
,
Integer
>
addressItem
=
lfuItemList
.
get
(
0
);
String
minAddress
=
addressItem
.
getKey
();
addressItem
.
setValue
(
addressItem
.
getValue
()
+
1
);
return
address
List
.
get
(
0
);
return
address
Item
.
getKey
(
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment