我正在实施一个简单的循环解决方案来实现负载平衡。这个想法是请求将带有一个整数值,并根据该值(如果它在范围内)我将选择正确的进程来转发请求。
创建地图
启动时,负载均衡器与每个进程通信并获取支持的范围。现在这个范围可以与另一个进程的范围重叠或者是一个子集。
例如,考虑以下
- 进程 A 支持 1-5
- 进程 B 支持 1-3
- 进程 C 支持 3-9
我最初的解决方案是创建一个简单的 HashMap>
。因此,我将循环遍历该范围并为同一进程创建多个条目。如果该键的条目已存在,我会将新进程添加到值列表中。
访问地图
当请求进入
- For 1-2 时,A 和 A 上的循环赛; B.
- 3 场比赛,A、B 和 B 循环赛。 C
- 为 3-5,B & 循环赛C
- 对于 6-9 岁,始终 C
问题 - 更新地图
我对此很满意,直到出现一个场景,其中我必须更新 Process 对象中的一些参数,这些参数将在将来的请求中考虑。因此,如果必须更新进程 C 的参数,我必须循环遍历映射的每个条目,对于每个条目,我必须循环遍历值列表并检查该进程是否存在于列表中。如果是,请更新列表,然后更新地图。这是一个 ConcurrentMap,这意味着当我循环时,我将在更新发生时锁定对地图的访问。
我尝试了不同的解决方案来提高更新效率,例如使用类 Range(int min, int max)
实现 Comparable
,然后使用 NavigableMap< ;范围,过程>
。但这并不能涵盖正确无间隙地循环的每个场景(重叠、范围的子集)。
避免更新循环会很好。有更好的解决方案的建议吗?
I am implementing a simple round robin solution for load balancing. The idea is a request would come in with a integer value and based on that value (if it is within Range) I will choose the correct process to forward the request.
Creation of Map
On startup, load balancer talks to every process and gets the supported range. Now this range can overlap another process's Range or be a subset.
For example, consider the following
- Process A supports 1-5
- Process B supports 1-3
- Process C supports 3-9
The initial solution I had was to create a simple HashMap<Integer, List<Process>>
. So I will loop through the range and create a multiple entries for the same process. If an entry already exists for the key, I will add the new process to value list.
Accessing Map
When a request comes in
- For 1-2, round robin on A & B.
- For 3, round robin on A,B & C
- For 3-5, round robin on B & C
- For 6-9, always C
Problem - Updating Map
I was fine with this until there came a scenario, wherein I have to update some parameters in the Process object which will be taken into consideration for future requests. So if have to update a parameter for Process C, I have to loop through every entry of the Map, for each entry I have to loop through value list and check whether that process exists in the list. If yes, update the list then update the map. And this is a ConcurrentMap, which means while I am looping through I will lock access to map while update happens.
I have tried different solutions to make the update more efficient like using a class Range(int min, int max)
that implements Comparable<Range>
and then have NavigableMap<Range, Process>
. But this does not cover every scenario (overlap, subset of Range) to round robin correctly without gaps.
Avoiding the loop for update will be nice. Any suggestions for better solution?
发布评论
评论(1)
你尝试过 LinkedHashMap 吗? treeMap 也可能对您的情况有所帮助。此外,还有一个公共集合 MultiKeyHashMap,允许您为单个值维护多个键。
Did u try LinkedHashMap? a treeMap may also help in your case. Further, there is a commons collection MultiKeyHashMap that allows you to maintain multiple keys for a single value.