You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sf...@apache.org on 2014/10/29 19:01:55 UTC

[1/2] git commit: fix gcm batching

Repository: incubator-usergrid
Updated Branches:
  refs/heads/two-dot-o a62df3a31 -> 610eb2da7


fix gcm batching


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/ca787926
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/ca787926
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/ca787926

Branch: refs/heads/two-dot-o
Commit: ca78792686f1cd06c984045d4a27921009134866
Parents: a62df3a
Author: Shawn Feldman <sf...@apache.org>
Authored: Wed Oct 29 11:44:51 2014 -0600
Committer: Shawn Feldman <sf...@apache.org>
Committed: Wed Oct 29 11:44:51 2014 -0600

----------------------------------------------------------------------
 .../services/notifications/gcm/GCMAdapter.java  | 24 +++++++++++---------
 1 file changed, 13 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/ca787926/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
index e0b32ee..e5867ea 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
@@ -16,6 +16,7 @@
  */
 package org.apache.usergrid.services.notifications.gcm;
 
+import com.clearspring.analytics.hash.MurmurHash;
 import com.google.android.gcm.server.*;
 import org.apache.usergrid.persistence.entities.Notification;
 import org.apache.usergrid.persistence.entities.Notifier;
@@ -34,6 +35,8 @@ import org.apache.usergrid.services.notifications.TaskTracker;
 
 import java.io.IOException;
 import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
 
 public class GCMAdapter implements ProviderAdapter {
 
@@ -43,11 +46,12 @@ public class GCMAdapter implements ProviderAdapter {
     private final Notifier notifier;
     private EntityManager entityManager;
 
-    private Map<Notifier, Batch> notifierBatches = new HashMap<>();
+    private ConcurrentHashMap<Long,Batch> batches;
 
     public GCMAdapter(EntityManager entityManager,Notifier notifier){
         this.notifier = notifier;
         this.entityManager = entityManager;
+        batches = new ConcurrentHashMap<>();
     }
     @Override
     public void testConnection() throws ConnectionException {
@@ -62,8 +66,7 @@ public class GCMAdapter implements ProviderAdapter {
     }
 
     @Override
-    public void sendNotification(String providerId,
-            Object payload, Notification notification, TaskTracker tracker)
+    public void sendNotification(String providerId, Object payload, Notification notification, TaskTracker tracker)
             throws Exception {
         Map<String,Object> map = (Map<String, Object>) payload;
         final String expiresKey = "time_to_live";
@@ -77,17 +80,18 @@ public class GCMAdapter implements ProviderAdapter {
     }
 
     synchronized private Batch getBatch( Map<String, Object> payload) {
-        Batch batch = notifierBatches.get(notifier);
+        long hash = MurmurHash.hash64(payload);
+        Batch batch = batches.get(hash);
         if (batch == null && payload != null) {
-            batch = new Batch(notifier, payload);
-            notifierBatches.put(notifier, batch);
+            batch = new Batch(notifier,payload);
+            batches.put(hash,batch);
         }
         return batch;
     }
 
     @Override
     synchronized public void doneSendingNotifications() throws Exception {
-        for (Batch batch : notifierBatches.values()) {
+        for (Batch batch : batches.values()) {
             batch.send();
         }
     }
@@ -95,9 +99,8 @@ public class GCMAdapter implements ProviderAdapter {
     @Override
     public void removeInactiveDevices( ) throws Exception {
         Batch batch = getBatch( null);
-        Map<String,Date> map = null;
         if(batch != null) {
-            map = batch.getAndClearInactiveDevices();
+            Map<String,Date> map = batch.getAndClearInactiveDevices();
             InactiveDeviceManager deviceManager = new InactiveDeviceManager(notifier,entityManager);
             deviceManager.removeInactiveDevices(map);
         }
@@ -147,7 +150,7 @@ public class GCMAdapter implements ProviderAdapter {
         private List<TaskTracker> trackers;
         private Map<String, Date> inactiveDevices = new HashMap<String, Date>();
 
-        Batch(Notifier notifier, Map<String, Object> payload) {
+        Batch(Notifier notifier, Map<String,Object> payload) {
             this.notifier = notifier;
             this.payload = payload;
             this.ids = new ArrayList<String>();
@@ -163,7 +166,6 @@ public class GCMAdapter implements ProviderAdapter {
         synchronized void add(String id, TaskTracker tracker) throws Exception {
             ids.add(id);
             trackers.add(tracker);
-
             if (ids.size() == BATCH_SIZE) {
                 send();
             }


[2/2] git commit: fix google push logic

Posted by sf...@apache.org.
fix google push logic


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/610eb2da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/610eb2da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/610eb2da

Branch: refs/heads/two-dot-o
Commit: 610eb2da7202a899d41637ddd513488861e2bb16
Parents: ca78792
Author: Shawn Feldman <sf...@apache.org>
Authored: Wed Oct 29 11:59:50 2014 -0600
Committer: Shawn Feldman <sf...@apache.org>
Committed: Wed Oct 29 11:59:50 2014 -0600

----------------------------------------------------------------------
 .../services/notifications/gcm/GCMAdapter.java  | 91 +++++++++++---------
 1 file changed, 52 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/610eb2da/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
----------------------------------------------------------------------
diff --git a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
index e5867ea..36cb7f0 100644
--- a/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
+++ b/stack/services/src/main/java/org/apache/usergrid/services/notifications/gcm/GCMAdapter.java
@@ -79,20 +79,24 @@ public class GCMAdapter implements ProviderAdapter {
         batch.add(providerId, tracker);
     }
 
-    synchronized private Batch getBatch( Map<String, Object> payload) {
-        long hash = MurmurHash.hash64(payload);
-        Batch batch = batches.get(hash);
-        if (batch == null && payload != null) {
-            batch = new Batch(notifier,payload);
-            batches.put(hash,batch);
+    private Batch getBatch( Map<String, Object> payload) {
+        synchronized (this) {
+            long hash = MurmurHash.hash64(payload);
+            Batch batch = batches.get(hash);
+            if (batch == null && payload != null) {
+                batch = new Batch(notifier, payload);
+                batches.put(hash, batch);
+            }
+            return batch;
         }
-        return batch;
     }
 
     @Override
-    synchronized public void doneSendingNotifications() throws Exception {
-        for (Batch batch : batches.values()) {
-            batch.send();
+    public void doneSendingNotifications() throws Exception {
+        synchronized (this) {
+            for (Batch batch : batches.values()) {
+                batch.send();
+            }
         }
     }
 
@@ -163,11 +167,18 @@ public class GCMAdapter implements ProviderAdapter {
             return map;
         }
 
-        synchronized void add(String id, TaskTracker tracker) throws Exception {
-            ids.add(id);
-            trackers.add(tracker);
-            if (ids.size() == BATCH_SIZE) {
-                send();
+        void add(String id, TaskTracker tracker) throws Exception {
+            synchronized (this) {
+                if(!ids.contains(id)) { //dedupe to a device
+                    ids.add(id);
+                    trackers.add(tracker);
+                    if (ids.size() == BATCH_SIZE) {
+                        send();
+                    }
+                }else{
+                    tracker.completed();
+                }
+
             }
         }
 
@@ -177,33 +188,35 @@ public class GCMAdapter implements ProviderAdapter {
         // anything that JSONValue can handle is fine.
         // (What is necessary here is that the Map needs to have a nested
         // structure.)
-        synchronized void send() throws Exception {
-            if (ids.size() == 0)
-                return;
-            Sender sender = new Sender(notifier.getApiKey());
-            Message.Builder builder = new Message.Builder();
-            builder.setData(payload);
-            Message message = builder.build();
-
-            MulticastResult multicastResult = sender.send(message, ids, SEND_RETRIES);
-            LOG.debug("sendNotification result: {}", multicastResult);
-
-            for (int i = 0; i < multicastResult.getResults().size(); i++) {
-                Result result = multicastResult.getResults().get(i);
-
-                if (result.getMessageId() != null) {
-                    String canonicalRegId = result.getCanonicalRegistrationId();
-                    trackers.get(i).completed(canonicalRegId);
-                } else {
-                    String error = result.getErrorCodeName();
-                    trackers.get(i).failed(error, error);
-                    if (Constants.ERROR_NOT_REGISTERED.equals(error) || Constants.ERROR_INVALID_REGISTRATION.equals(error)) {
-                        inactiveDevices.put(ids.get(i), new Date());
+        void send() throws Exception {
+            synchronized (this) {
+                if (ids.size() == 0)
+                    return;
+                Sender sender = new Sender(notifier.getApiKey());
+                Message.Builder builder = new Message.Builder();
+                builder.setData(payload);
+                Message message = builder.build();
+
+                MulticastResult multicastResult = sender.send(message, ids, SEND_RETRIES);
+                LOG.debug("sendNotification result: {}", multicastResult);
+
+                for (int i = 0; i < multicastResult.getResults().size(); i++) {
+                    Result result = multicastResult.getResults().get(i);
+
+                    if (result.getMessageId() != null) {
+                        String canonicalRegId = result.getCanonicalRegistrationId();
+                        trackers.get(i).completed(canonicalRegId);
+                    } else {
+                        String error = result.getErrorCodeName();
+                        trackers.get(i).failed(error, error);
+                        if (Constants.ERROR_NOT_REGISTERED.equals(error) || Constants.ERROR_INVALID_REGISTRATION.equals(error)) {
+                            inactiveDevices.put(ids.get(i), new Date());
+                        }
                     }
                 }
+                this.ids.clear();
+                this.trackers.clear();
             }
-            this.ids.clear();
-            this.trackers.clear();
         }
     }
 }