이제 cluster topology의 변화에 대해서 어떻게 처리할 수 있는지 알게 되었다. 마찬가지로 cluster 내의 data 변화에 대해서도 처리할 수 있다. 이것이 어떻게 동작하는지 더 잘 보여주기 위해서 2개의 node의 역할을 분리할 것이다. Coordinator에 data를 put하고, 다른 node에서는 cache의 변화된 내용을 보여줄 것이다.
@Listener(clustered = true)
public class CacheListener {
@CacheEntryCreated
public void entryCreated(CacheEntryCreatedEvent<String,
LocationWeather> event) {
if (!event.isOriginLocal())
{
System.out.printf("-- Entry for %s modified by another node
in the cluster\n", event.getKey());
}
}
}
일반적으로 cache listener는 단지 "local events"에 대해서만 listen할 수 있다. 예를 들어, 동일 node에서 발생한 event에 대해서만 listen 가능하다. 그러나, 이번 예제에서처럼 모든 node에서 발생한 event를 listen하고 싶다면 "clustered = true" annotation parameter를 사용하여 모든 node의 event를 listen할 수 있다.
Event notification을 살펴보자.
git checkout -f step-7
mvn clean package exec:exec # from terminal 1
mvn exec:exec # from terminal 2
[Coordinator Output : PUT]
---- View changed: [hoth-1712]
----
---- Waiting for cluster to form ----
---- View changed: [hoth-1712, hoth-9822] ----
---- Fetching weather information ----
-- Entry for Rome, Italy modified by another node in the cluster
Rome, Italy - Temperature: 11.6° C, Conditions: Sky is Clear
Como, Italy - Temperature: 5.6° C, Conditions: Sky is Clear
-- Entry for Basel, Switzerland modified by another node in the
cluster
Basel, Switzerland - Temperature: 2.9° C, Conditions: broken clouds
Bern, Switzerland - Temperature: -2.1° C, Conditions: scattered clouds
-- Entry for London, UK modified by another node in the cluster
London, UK - Temperature: 4.8° C, Conditions: light rain
-- Entry for Newcastle, UK modified by another node in the
cluster
Newcastle, UK - Temperature: 1.8° C, Conditions: Sky is Clear
-- Entry for Bucharest, Romania modified by another node in the
cluster
Bucharest, Romania - Temperature: 8.4° C, Conditions: few clouds
Cluj-Napoca, Romania - Temperature: 5.8° C, Conditions: broken clouds
-- Entry for Ottawa, Canada modified by another node in the
cluster
Ottawa, Canada - Temperature: -12.3° C, Conditions: overcast clouds
-- Entry for Toronto, Canada modified by another node in the
cluster
Toronto, Canada - Temperature: -10.1° C, Conditions: few clouds
Lisbon, Portugal - Temperature: 15.0° C, Conditions: light rain
-- Entry for Porto, Portugal modified by another node in the cluster
Porto, Portugal - Temperature: 12.1° C, Conditions: moderate rain
Raleigh, USA - Temperature: 6.0° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.5° C, Conditions: light rain
---- Fetched in 5466ms ----
---- Fetching weather information ----
Rome, Italy - Temperature: 11.6° C, Conditions: Sky is Clear
Como, Italy - Temperature: 5.6° C, Conditions: Sky is Clear
Basel, Switzerland - Temperature: 2.9° C, Conditions: broken clouds
Bern, Switzerland - Temperature: -2.1° C, Conditions: scattered
clouds
London, UK - Temperature: 4.8° C, Conditions: light rain
Newcastle, UK - Temperature: 1.8° C, Conditions: Sky is Clear
Bucharest, Romania - Temperature: 8.4° C, Conditions: few clouds
Cluj-Napoca, Romania - Temperature: 5.8° C, Conditions: broken
clouds
Ottawa, Canada - Temperature: -12.3° C, Conditions: overcast
clouds
Toronto, Canada - Temperature: -10.1° C, Conditions: few clouds
Lisbon, Portugal - Temperature: 15.0° C, Conditions: light rain
Porto, Portugal - Temperature: 12.1° C, Conditions: moderate rain
Raleigh, USA - Temperature: 6.0° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.5° C, Conditions: light rain
---- Fetched in 2ms ----
---- Fetching weather information ----
-- Entry for Rome, Italy modified by another node in the cluster
Rome, Italy - Temperature: 11.6° C, Conditions: Sky is Clear
Como, Italy - Temperature: 5.6° C, Conditions: Sky is Clear
-- Entry for Basel, Switzerland modified by another node in the
cluster
Basel, Switzerland - Temperature: 2.9° C, Conditions: broken clouds
Bern, Switzerland - Temperature: -2.1° C, Conditions: scattered clouds
-- Entry for London, UK modified by another node in the cluster
London, UK - Temperature: 4.8° C, Conditions: light rain
-- Entry for Newcastle, UK modified by another node in the
cluster
Newcastle, UK - Temperature: 1.8° C, Conditions: Sky is Clear
-- Entry for Bucharest, Romania modified by another node in the
cluster
Bucharest, Romania - Temperature: 8.4° C, Conditions: few
clouds
Cluj-Napoca, Romania - Temperature: 5.8° C, Conditions: broken clouds
-- Entry for Ottawa, Canada modified by another node in the
cluster
Ottawa, Canada - Temperature: -12.3° C, Conditions: overcast clouds
-- Entry for Toronto, Canada modified by another node in the
cluster
Toronto, Canada - Temperature: -10.1° C, Conditions: few clouds
Lisbon, Portugal - Temperature: 15.0° C, Conditions: light rain
-- Entry for Porto, Portugal modified by another node in the
cluster
Porto, Portugal - Temperature: 12.1° C, Conditions: moderate rain
Raleigh, USA - Temperature: 6.0° C, Conditions: Sky is Clear
Washington, USA - Temperature: 3.5° C, Conditions: light rain
---- Fetched in 1248ms ----
[Other Output : Event View]
---- View changed: [hoth-1712,
hoth-9822] ----
---- Waiting for cluster to form ----
-- Entry for Rome, Italy modified by another node in the cluster
-- Entry for Como, Italy modified by another node in the cluster
-- Entry for Basel, Switzerland modified by another node in the cluster
-- Entry for Bern, Switzerland modified by another node in the cluster
-- Entry for London, UK modified by another node in the cluster
-- Entry for Newcastle, UK modified by another node in the cluster
-- Entry for Bucharest, Romania modified by another node in the cluster
-- Entry for Cluj-Napoca, Romania modified by another node in the
cluster
-- Entry for Ottawa, Canada modified by another node in the cluster
-- Entry for Toronto, Canada modified by another node in the cluster
-- Entry for Lisbon, Portugal modified by another node in the cluster
-- Entry for Porto, Portugal modified by another node in the cluster
-- Entry for Raleigh, USA modified by another node in the cluster
-- Entry for Washington, USA modified by another node in the cluster
-- Entry for Rome, Italy modified by another node in the cluster
-- Entry for Como, Italy modified by another node in the cluster
-- Entry for Basel, Switzerland modified by another node in the cluster
-- Entry for Bern, Switzerland modified by another node in the cluster
-- Entry for London, UK modified by another node in the cluster
-- Entry for Newcastle, UK modified by another node in the cluster
-- Entry for Bucharest, Romania modified by another node in the cluster
-- Entry for Cluj-Napoca, Romania modified by another node in the
cluster
-- Entry for Ottawa, Canada modified by another node in the cluster
-- Entry for Toronto, Canada modified by another node in the cluster
-- Entry for Lisbon, Portugal modified by another node in the cluster
-- Entry for Porto, Portugal modified by another node in the cluster
-- Entry for Raleigh, USA modified by another node in the cluster
-- Entry for Washington, USA modified by another node in the cluster
---- View changed: [hoth-9822] ----
실제로 cache에 작업을 수행하는 coordinator에서 남기는 event log와 another node에서 entrries가 수정되었음을 알리는 event log를 볼 수 있을 것이다. 이것은 consistent hashing algorithm에 의해서 entry가 어느 node에 원본이 저장되어 있는지를 설명한다. 다음 단계에서는 분산에 대해 어떻게 제어할 수 있는지에 대해서 설명할 것이다.
댓글을 달아 주세요
댓글 RSS 주소 : http://www.yongbi.net/rss/comment/677