Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Initializing DataNode");
            string nameNodeIp = args[0];

            int port = Convert.ToInt32(args[1]);

            // Use ec2 instance manager to get the private ip address of this data node
            EC2InstanceManager.InstanceManager instanceManager = EC2InstanceManager.InstanceManager.Instance;
            ipAddress = instanceManager.GetPrivateIpAddress();

#if DEBUG
            if (ipAddress == null)
            {
                ipAddress = "localhost";
            }
#endif

            Server server = new Server
            {
                Services = { DataNodeProto.DataNodeProto.BindService(new DataNodeHandler()),
                             ClientProto.ClientProto.BindService(new ClientHandler()) },
                //Ports = { new ServerPort(ipAddress, Constants.Port, ServerCredentials.Insecure) }
                Ports = { new ServerPort(ipAddress, port, ServerCredentials.Insecure) }
            };

            Console.WriteLine("Done initializing");

            server.Start();

            Console.WriteLine("Trying to connect to: " + nameNodeIp);
            Channel channel = new Channel(nameNodeIp + ":" + Constants.Port, ChannelCredentials.Insecure);
            ConnectionManager.Instance.NameNodeConnection = channel;
            var client = new DataNodeProto.DataNodeProto.DataNodeProtoClient(channel);

            // Send initial heartbeat to initialize datanode in namenode
            var response = client.SendHeartBeat(HeartBeat.CreateHeartBeatRequest(), new CallOptions().WithWaitForReady(true));
            Console.WriteLine("Successfully connected to namenode");

            // Initialize blockstorage
            BlockStorage mBlockStorage = BlockStorage.Instance;

            Task heartBeatTask   = HeartBeat.SendHeartBeat(client);
            Task blockReportTask = BlockReport.SendBlockReport(client);
            while (true)
            {
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Send blockreport
        /// </summary>
        /// <param name="client">Datanode grpc client</param>
        public static async Task SendHeartBeat(DataNodeProto.DataNodeProto.DataNodeProtoClient client)
        {
            while (true)
            {
                DataNodeProto.HeartBeatRequest heartBeatRequest = CreateHeartBeatRequest();

                try
                {
                    DataNodeProto.HeartBeatResponse response         = client.SendHeartBeat(heartBeatRequest);
                    DataNodeProto.BlockCommand      nameNodeCommands = response.Commands;
                    switch (nameNodeCommands.Action)
                    {
                    case DataNodeProto.BlockCommand.Types.Action.Transfer:
                        foreach (var block in nameNodeCommands.DataBlock.ToList())
                        {
                            // Get block data
                            byte[] blockData = BlockStorage.Instance.ReadBlock(Guid.Parse(block.BlockId.Value));

                            if (blockData != null)
                            {
                                Metadata metaData = new Metadata {
                                    new Metadata.Entry("blockid", block.BlockId.Value),
                                    new Metadata.Entry("blocksize", blockData.Length.ToString())
                                };

                                // Send data to each block
                                foreach (var dataNode in block.DataNodes)
                                {
                                    Task task = ForwardBlock(dataNode, blockData, metaData);
                                }
                            }
                        }
                        break;

                    case DataNodeProto.BlockCommand.Types.Action.Delete:
                        InvalidateBlocks(nameNodeCommands.BlockList);
                        break;
                    }
                }
                catch (RpcException e)
                {
                    Console.WriteLine("HeartBeat failed: " + e.Message);
                }
                await Task.Delay(Constants.HeartBeatInterval); // This is an HDFS default
            }
        }