Example #1
0
 @Override
 public void writeData(ObjectDataOutput out) throws IOException {
   super.writeData(out);
   dataKey.writeData(out);
   IOUtil.writeNullableData(out, dataNewValue);
   IOUtil.writeNullableData(out, dataOldValue);
 }
 public void readData(ObjectDataInput in) throws IOException {
   proxyId = new CollectionProxyId();
   proxyId.readData(in);
   operationFactoryType = OperationFactoryType.getByType(in.readInt());
   key = IOUtil.readNullableData(in);
   value = IOUtil.readNullableData(in);
 }
Example #3
0
 @Override
 public void readData(ObjectDataInput in) throws IOException {
   super.readData(in);
   dataKey = IOUtil.readData(in);
   dataNewValue = IOUtil.readNullableData(in);
   dataOldValue = IOUtil.readNullableData(in);
 }
 public void readData(ObjectDataInput in) throws IOException {
   int size = in.readInt();
   map = new HashMap<Data, Collection<Data>>(size);
   for (int i = 0; i < size; i++) {
     Data key = IOUtil.readData(in);
     int collSize = in.readInt();
     Collection coll = new ArrayList(collSize);
     for (int j = 0; j < collSize; j++) {
       coll.add(IOUtil.readData(in));
     }
     map.put(key, coll);
   }
 }
 public void run() {
   while (!Thread.currentThread().isInterrupted()) {
     try {
       if (conn == null) {
         try {
           conn = pickConnection();
         } catch (Exception e) {
           logger.severe("Error while connecting to cluster!", e);
           client.getLifecycleService().shutdown();
           return;
         }
       }
       loadInitialMemberList();
       listenMembershipEvents();
     } catch (Exception e) {
       if (client.getLifecycleService().isRunning()) {
         if (logger.isFinestEnabled()) {
           logger.warning("Error while listening cluster events! -> " + conn, e);
         } else {
           logger.warning(
               "Error while listening cluster events! -> " + conn + ", Error: " + e.toString());
         }
       }
       IOUtil.closeResource(conn);
       conn = null;
       fireConnectionEvent(true);
     }
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
       break;
     }
   }
 }
Example #6
0
 private static Set<ServiceDefinition> parse(URLDefinition urlDefinition) {
   try {
     final Set<ServiceDefinition> names = new HashSet<ServiceDefinition>();
     BufferedReader r = null;
     try {
       URL url = urlDefinition.uri.toURL();
       r = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
       while (true) {
         String line = r.readLine();
         if (line == null) {
           break;
         }
         int comment = line.indexOf('#');
         if (comment >= 0) {
           line = line.substring(0, comment);
         }
         String name = line.trim();
         if (name.length() == 0) {
           continue;
         }
         names.add(new ServiceDefinition(name, urlDefinition.classLoader));
       }
     } finally {
       IOUtil.closeResource(r);
     }
     return names;
   } catch (Exception e) {
     LOGGER.severe(e);
   }
   return Collections.emptySet();
 }
 protected void writeInternal(ObjectDataOutput out) throws IOException {
   out.writeUTF(name);
   dataKey.writeData(out);
   out.writeLong(threadId);
   IOUtil.writeNullableData(out, dataValue);
   out.writeLong(ttl);
 }
 protected void readInternal(ObjectDataInput in) throws IOException {
   name = in.readUTF();
   dataKey = new Data();
   dataKey.readData(in);
   threadId = in.readLong();
   dataValue = IOUtil.readNullableData(in);
   ttl = in.readLong();
 }
    private void close() {
      final ClientConnection currentOwnerConnection = ownerConnection;
      if (currentOwnerConnection == null) {
        return;
      }

      IOUtil.closeResource(currentOwnerConnection);
      markAsClosed();
    }
 private void deleteCacheResources(String name) {
   Set<Closeable> cacheResources = resources.remove(name);
   if (cacheResources != null) {
     for (Closeable resource : cacheResources) {
       IOUtil.closeResource(resource);
     }
     cacheResources.clear();
   }
 }
 public byte[] readByteArray(String fieldName) throws IOException {
   final int currentPos = in.position();
   try {
     int pos = readPosition(fieldName, FieldType.BYTE_ARRAY);
     in.position(pos);
     return IOUtil.readByteArray(in);
   } finally {
     in.position(currentPos);
   }
 }
 @Override
 public boolean deregisterListener(String name, String registrationId) {
   final EventService eventService = getNodeEngine().getEventService();
   boolean result = eventService.deregisterListener(SERVICE_NAME, name, registrationId);
   Closeable listener = closeableListeners.remove(registrationId);
   if (listener != null) {
     IOUtil.closeResource(listener);
   }
   return result;
 }
Example #13
0
 void copy(ByteBuffer cb) {
   if (cb.isDirect()) {
     int n = Math.min(cb.remaining(), bbValue.remaining());
     if (n > 0) {
       cb.get(bbValue.array(), bbValue.position(), n);
       bbValue.position(bbValue.position() + n);
     }
   } else {
     IOUtil.copyToHeapBuffer(cb, bbValue);
   }
 }
Example #14
0
  public Map<String, String> phoneHome(Node hazelcastNode, String version, boolean isEnterprise) {

    String downloadId = "source";
    InputStream is = null;
    try {
      is = getClass().getClassLoader().getResourceAsStream("hazelcast-download.properties");
      if (is != null) {
        final Properties properties = new Properties();
        properties.load(is);
        downloadId = properties.getProperty("hazelcastDownloadId");
      }
    } catch (IOException ignored) {
      EmptyStatement.ignore(ignored);
    } finally {
      IOUtil.closeResource(is);
    }

    // Calculate native memory usage from native memory config
    NativeMemoryConfig memoryConfig = hazelcastNode.getConfig().getNativeMemoryConfig();
    final ClusterServiceImpl clusterService = hazelcastNode.getClusterService();
    long totalNativeMemorySize =
        clusterService.getSize(DATA_MEMBER_SELECTOR) * memoryConfig.getSize().bytes();
    String nativeMemoryParameter =
        (isEnterprise) ? Long.toString(MemoryUnit.BYTES.toGigaBytes(totalNativeMemorySize)) : "0";
    // Calculate connected clients to the cluster.
    Map<ClientType, Integer> clusterClientStats =
        hazelcastNode.clientEngine.getConnectedClientStats();
    RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();

    Long clusterUpTime = clusterService.getClusterClock().getClusterUpTime();

    PhoneHomeParameterCreator parameterCreator = new PhoneHomeParameterCreator();
    parameterCreator.addParam("version", version);
    parameterCreator.addParam("m", hazelcastNode.getLocalMember().getUuid());
    parameterCreator.addParam("e", Boolean.toString(isEnterprise));
    parameterCreator.addParam("l", MD5Util.toMD5String(hazelcastNode.getConfig().getLicenseKey()));
    parameterCreator.addParam("p", downloadId);
    parameterCreator.addParam("c", clusterService.getClusterId());
    parameterCreator.addParam("crsz", convertToLetter(clusterService.getMembers().size()));
    parameterCreator.addParam(
        "cssz", convertToLetter(hazelcastNode.clientEngine.getClientEndpointCount()));
    parameterCreator.addParam("hdgb", nativeMemoryParameter);
    parameterCreator.addParam("ccpp", Integer.toString(clusterClientStats.get(ClientType.CPP)));
    parameterCreator.addParam("cdn", Integer.toString(clusterClientStats.get(ClientType.CSHARP)));
    parameterCreator.addParam("cjv", Integer.toString(clusterClientStats.get(ClientType.JAVA)));
    parameterCreator.addParam("cuptm", Long.toString(clusterUpTime));
    parameterCreator.addParam("nuptm", Long.toString(runtimeMxBean.getUptime()));
    String urlStr = BASE_PHONE_HOME_URL + parameterCreator.build();
    fetchWebService(urlStr);

    return parameterCreator.getParameters();
  }
 public void destroy() {
   active = false;
   for (SerializerAdapter serializer : typeMap.values()) {
     serializer.destroy();
   }
   typeMap.clear();
   idMap.clear();
   global.set(null);
   constantTypesMap.clear();
   for (BufferObjectDataOutput output : outputPool) {
     IOUtil.closeResource(output);
   }
   outputPool.clear();
 }
  @Override
  public void onDetectingUnresponsiveConnection(ClientConnection connection) {
    if (smartRouting) {
      // closing the owner connection if unresponsive so that it can be switched to a healthy one.
      ownerConnectionFuture.closeIfAddressMatches(connection.getEndPoint());
      // we do not close connection itself since we will continue to send heartbeat ping to this
      // connection.
      // IOUtil.closeResource(connection);
      return;
    }

    // close both owner and operation connection
    ownerConnectionFuture.close();
    IOUtil.closeResource(connection);
  }
Example #17
0
 private void fetchWebService(String urlStr) {
   InputStream in = null;
   try {
     URL url = new URL(urlStr);
     URLConnection conn = url.openConnection();
     conn.setRequestProperty("User-Agent", "Mozilla/5.0");
     conn.setConnectTimeout(TIMEOUT * 2);
     conn.setReadTimeout(TIMEOUT * 2);
     in = new BufferedInputStream(conn.getInputStream());
   } catch (IOException ignored) {
     EmptyStatement.ignore(ignored);
   } finally {
     IOUtil.closeResource(in);
   }
 }
  private void _sendAndHandle(
      ConnectionFactory connectionFactory, Object obj, ResponseHandler handler) throws IOException {
    ResponseStream stream = null;
    while (stream == null) {
      if (!active) {
        throw new HazelcastInstanceNotActiveException();
      }
      Connection conn = null;
      try {
        conn = connectionFactory.create();
        final SerializationService serializationService = getSerializationService();
        final Data request = serializationService.toData(obj);
        conn.write(request);
        stream = new ResponseStreamImpl(serializationService, conn);
      } catch (Exception e) {
        if (e instanceof IOException) {
          if (logger.isFinestEnabled()) {
            logger.finest("Error on connection... conn: " + conn + ", error: " + e);
          }
        }
        if (conn != null) {
          IOUtil.closeResource(conn);
        }
        if (ErrorHandler.isRetryable(e)) {
          if (redoOperation || obj instanceof RetryableRequest) {
            if (logger.isFinestEnabled()) {
              logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
            }
            beforeRetry();
            continue;
          }
        }
        if (e instanceof IOException && !active) {
          continue;
        }
        throw ExceptionUtil.rethrow(e, IOException.class);
      }
    }

    try {
      handler.handle(stream);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e, IOException.class);
    } finally {
      stream.end();
    }
  }
 private <T> T _sendAndReceive(ConnectionFactory connectionFactory, Object obj)
     throws IOException {
   while (active) {
     Connection conn = null;
     boolean release = true;
     try {
       conn = connectionFactory.create();
       final SerializationService serializationService = getSerializationService();
       final Data request = serializationService.toData(obj);
       conn.write(request);
       final Data response = conn.read();
       final Object result = serializationService.toObject(response);
       return ErrorHandler.returnResultOrThrowException(result);
     } catch (Exception e) {
       if (e instanceof IOException) {
         if (logger.isFinestEnabled()) {
           logger.finest("Error on connection... conn: " + conn + ", error: " + e);
         }
         IOUtil.closeResource(conn);
         release = false;
       }
       if (ErrorHandler.isRetryable(e)) {
         if (redoOperation || obj instanceof RetryableRequest) {
           if (logger.isFinestEnabled()) {
             logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
           }
           beforeRetry();
           continue;
         }
       }
       if (e instanceof IOException && !active) {
         continue;
       }
       throw ExceptionUtil.rethrow(e, IOException.class);
     } finally {
       if (release && conn != null) {
         conn.release();
       }
     }
   }
   throw new HazelcastInstanceNotActiveException();
 }
 @Override
 public void deregisterAllListener(String name) {
   final EventService eventService = getNodeEngine().getEventService();
   final Collection<EventRegistration> registrations =
       eventService.getRegistrations(SERVICE_NAME, name);
   if (registrations != null) {
     for (EventRegistration registration : registrations) {
       Closeable listener = closeableListeners.remove(registration.getId());
       if (listener != null) {
         IOUtil.closeResource(listener);
       }
     }
   }
   eventService.deregisterAllListeners(AbstractCacheService.SERVICE_NAME, name);
   CacheContext cacheContext = cacheContexts.get(name);
   if (cacheContext != null) {
     cacheContext.resetCacheEntryListenerCount();
     cacheContext.resetInvalidationListenerCount();
   }
 }
 public boolean doActualRead(ByteBuffer cb) {
   if (readyToReadData) {
     if (chunked && (data == null || !data.hasRemaining())) {
       boolean hasLine = readLine(cb);
       String lineStr = null;
       if (hasLine) {
         lineStr = toStringAndClear(line).trim();
       }
       if (hasLine) {
         int dataSize = lineStr.length() == 0 ? 0 : Integer.parseInt(lineStr, 16); // hex string
         if (dataSize == 0) {
           return true;
         }
         if (data != null) {
           ByteBuffer newData = ByteBuffer.allocate(data.capacity() + dataSize);
           newData.put(data.array());
           data = newData;
         } else {
           data = ByteBuffer.allocate(dataSize);
         }
       }
     }
     IOUtil.copyToHeapBuffer(cb, data);
   }
   while (!readyToReadData && cb.hasRemaining()) {
     byte b = cb.get();
     char c = (char) b;
     if (c == '\n') {
       processLine(toStringAndClear(line).toLowerCase());
       if (nextLine) {
         readyToReadData = true;
       }
       nextLine = true;
     } else if (c != '\r') {
       nextLine = false;
       line.put(b);
     }
   }
   return !chunked && ((data != null) && !data.hasRemaining());
 }
  public void run() {
    while (!Thread.currentThread().isInterrupted()) {
      try {
        if (conn == null) {
          try {
            conn = connectToOne();
          } catch (Exception e) {
            LOGGER.severe("Error while connecting to cluster!", e);
            client.getLifecycleService().shutdown();
            latch.countDown();
            return;
          }
        }
        getInvocationService().triggerFailedListeners();
        loadInitialMemberList();
        listenMembershipEvents();
      } catch (Exception e) {
        if (client.getLifecycleService().isRunning()) {
          if (LOGGER.isFinestEnabled()) {
            LOGGER.warning("Error while listening cluster events! -> " + conn, e);
          } else {
            LOGGER.warning(
                "Error while listening cluster events! -> " + conn + ", Error: " + e.toString());
          }
        }

        connectionManager.markOwnerConnectionAsClosed();
        IOUtil.closeResource(conn);
        conn = null;
        clusterService.fireConnectionEvent(true);
      }
      try {
        Thread.sleep(SLEEP_TIME);
      } catch (InterruptedException e) {
        latch.countDown();
        break;
      }
    }
  }
 @Override
 protected Class<?> findClass(String name) throws ClassNotFoundException {
   if (!wellKnownClasses.contains(name)) {
     return super.findClass(name);
   }
   String path = name.replace('.', '/') + ".class";
   InputStream in = null;
   try {
     in = getParent().getResourceAsStream(path);
     ByteArrayOutputStream bout = new ByteArrayOutputStream();
     byte[] buf = new byte[1024];
     int read;
     while ((read = in.read(buf)) != -1) {
       bout.write(buf, 0, read);
     }
     byte[] code = bout.toByteArray();
     return defineClass(name, code, 0, code.length);
   } catch (IOException e) {
     return super.findClass(name);
   } finally {
     IOUtil.closeResource(in);
   }
 }
 protected void readInternal(ObjectDataInput in) throws IOException {
   super.readInternal(in);
   recordId = in.readLong();
   index = in.readInt();
   value = IOUtil.readData(in);
 }
 public void writeData(ObjectDataOutput out) throws IOException {
   proxyId.writeData(out);
   out.writeInt(operationFactoryType.type);
   IOUtil.writeNullableData(out, key);
   IOUtil.writeNullableData(out, value);
 }
 public void close() {
   logger.info("Closing connection -> " + connection);
   IOUtil.closeResource(connection);
 }
 protected void closeCacheLoader() {
   // close the configured CacheLoader
   if (cacheLoader instanceof Closeable) {
     IOUtil.closeResource((Closeable) cacheLoader);
   }
 }